$a = date('Y-m-d h:i:s');
echo $a;
result:
2017-08-18 09:14:02
Is there a way to get the date in the same format using javascript / jquery ?
$a = date('Y-m-d h:i:s');
echo $a;
result:
2017-08-18 09:14:02
Is there a way to get the date in the same format using javascript / jquery ?
Here's an approach that is easy to understand for newer JavaScript developers who come from an OOP background.
var date = new Date();
alert(date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + " " + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2) + ":" + ("0" + date.getSeconds()).slice(-2));
date.getMonth() + 1 because the months are 0 indexed.
EDIT: The above solution now adds the leading zeros to getMonth() and getDay(). The slice(-2) call is a common way to getting the last two characters from the string.
For example, if date.getMonth() returns a 9. I would get 09, and slice(-2) would return me the same 09.
But if date.getMonth() returns a 10. I would get 010, and slice(-2) would return the last two characters again. So, 10.
The other answers are correct, this one is just easier to understand from a beginners perspective.
Try this
var date = new Date();
var iso = date.toISOString().match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/)
var correctdate = iso[1] + ' ' + iso[2];
var DateWithTime = new Date();
console.log(DateWithTime); //Date 2017-08-18T17:29:34.660Z
var onlyDate = DateWithTime.toISOString().substring(0, 10);
console.log(onlyDate); //2017-08-18