I have a script that prints the current date and time in JavaScript, but the DATE
is always 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
The Clock. systemUTC(). instant() method returns the current date and time both.
Use Date. now() to get the current timestamp fast.
.getMonth()
returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth()
in may will return 4
and not 5
.
So in your code we can use currentdate.getMonth()+1
to output the correct value. In addition:
.getDate()
returns the day of the month <- this is the one you want .getDay()
is a separate method of the Date
object which will return an integer representing the current day of the week (0-6) 0 == Sunday
etcso your code should look like this:
var currentdate = new Date(); var datetime = "Last Sync: " + currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
JavaScript Date instances inherit from Date.prototype. You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances
You can make use of the Date
prototype object to create a new method which will return today's date and time. These new methods or properties will be inherited by all instances of the Date
object thus making it especially useful if you need to re-use this functionality.
// For todays date; Date.prototype.today = function () { return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear(); } // For the time now Date.prototype.timeNow = function () { return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds(); }
You can then simply retrieve the date and time by doing the following:
var newDate = new Date(); var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();
Or call the method inline so it would simply be -
var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();
To get time and date you should use
new Date().toLocaleString(); >> "09/08/2014, 2:35:56 AM"
To get only the date you should use
new Date().toLocaleDateString(); >> "09/08/2014"
To get only the time you should use
new Date().toLocaleTimeString(); >> "2:35:56 AM"
Or if you just want the time in the format hh:mm
without AM/PM for US English
new Date().toLocaleTimeString('en-US', { hour12: false, hour: "numeric", minute: "numeric"}); >> "02:35"
or for British English
new Date().toLocaleTimeString('en-GB', { hour: "numeric", minute: "numeric"}); >> "02:35"
Read more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With