0
$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 ?

  • Use [momentjs](https://momentjs.com/) - `moment(someDate).format("YYYY-MM-DD HH:mm:ss")` – Rob M. Aug 18 '17 at 17:27
  • Check out the answers to [this question](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) for some examples on formatting a `Date` object in javascript – rickdenhaan Aug 18 '17 at 17:28
  • https://jsfiddle.net/7hefh2ef/ – blex Aug 18 '17 at 17:31
  • @Derek, accepted answer on your link does not answer my question and I don't want to read a long list of 35 other answers to get a clue. Maybe you should redirect me to a google search, it's the same. –  Aug 18 '17 at 19:31
  • Your question matches the question linked as a duplicate of. Although the "date formats" aren't exactly the same, they do not affect the mean for solving the problem. If you are unsatisfied with the answers you found in the linked question, feel free to create a new one over there. – Derek 朕會功夫 Aug 18 '17 at 19:45

3 Answers3

1

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.

Jimenemex
  • 3,104
  • 3
  • 24
  • 56
  • @Puerto You can very well put this inside its own function and use it like a simple function. The only difference is, somebody else already did it for you in PHP. :] – Jimenemex Aug 18 '17 at 18:04
  • incorrect result, its not current date - `2017-08-06 20:3:35`. Also, there is no zero in minutes. –  Aug 18 '17 at 18:05
  • You just add `("0" + (date.getMinutes()).slice(-2)`. Also I changed `getDay()` to `getDate()`. Which returns the actual date. `getDay()` returns the number of day in the week. (eg. Sunday = 0) – Jimenemex Aug 18 '17 at 18:13
0

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];
Peter M
  • 1,059
  • 8
  • 19
0
    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
Ankit Sahay
  • 1,710
  • 8
  • 14