I have a script that prints the current date and time in JavaScript, but the DATE
is allways wrong. Here is the code:
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDay() + "/"+currentdate.getMonth()
+ "/" + currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds();
It should print 18/04/2012 15:07:33
and prints 3/3/2012 15:07:33
Any help? Thanks
I needed to figure this out for a slate in after effects. Here's what I came up with after taking elements from a few different sources -- Formatting is MM/DD/YYYY HH:MM AM/PM
getDay()
gets the day of the week.3
is Wednesday. You wantgetDate()
, that will return18
.Also
getMonth()
starts at0
, you need to add1
to get4
(April).DEMO: http://jsfiddle.net/4zVxp/
To get time and date you should use
To get only the date you should use
To get only the time you should use
Or if you just want the time in the format
hh:mm
without AM/PM for US Englishor for British English
Read more here.