I want to get the current datetime in the format 2015-09-24 09:30:30
with jquery, is there anyway to do this? I tried $.now()
and got 1443085060076
, with Date($.now())
I got Thu Sep 24 2015 10:59:32 GMT+0200 (Mitteleuropäische Sommerzeit)
. Both not what I want.
Asked
Active
Viewed 2.6k times
1

yangsunny
- 656
- 5
- 13
- 32
-
The duplicated is including a new plugin. If you don't want to include it, see my answer. – Marcos Pérez Gude Sep 24 '15 at 09:06
2 Answers
9
It's simple:
var d = new Date($.now());
alert(d.getDate()+"-"+(d.getMonth() + 1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
See the API:
https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Date
See it working:

Marcos Pérez Gude
- 21,869
- 4
- 38
- 69
-
1thanks, works fine. except it show month and day in single digit. I found another way to get what I want `var d = new Date().toISOString().substr(0,19).replace('T',' ');` – yangsunny Sep 24 '15 at 09:13
-
Ok, so my answer is an example to show how to format easily a date. If this answer is useful for you please, upvote it. – Marcos Pérez Gude Sep 24 '15 at 09:51
-
1`d.getDay()` is the number of the day of the week and should be `d.getDate()` for the number of day in the month. `d.getMonth()` should be `(d.getMonth()+1)` because `getMonth` counts from 0. – Fid Oct 11 '18 at 22:22
-
-
@MarcosPérezGude You still have `d.getDay()` instead of `d.getDate()`. Today is `17-10-2018` and the above code would return `3-10-2018`, because it is the third day of the week. – Fid Oct 17 '18 at 12:14
4
Straight JS will work to. You don't need to use jQuery.
var d = new Date();
var o = {year:'numeric', month:'2-digit', day:'2-digit', hour:'2-digit', minute:'2-digit', second:'2-digit'};
d.toLocaleDateString('en-US', o);
More information on the toLocaleDateString method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Jason
- 111
- 2
-
-
Good appointment. I upvote you because I didn't know this technique. Thank you! – Marcos Pérez Gude Sep 24 '15 at 10:16