They both serve different purposes.
performance.now() is relative to page load and more precise in orders of magnitude. Use cases include benchmarking and other cases where a high-resolution time is required such as media (gaming, audio, video, etc.)
It should be noted that performance.now() is available in IE10 or later browsers. See caniuse page.
Date.now() is relative to the Unix epoch (1970-01-01T00:00:00Z) and dependent on system clock. Use cases include same old date manipulation ever since the beginning of JavaScript.
See When milliseconds are not enough: performance.now and now method (Internet Explorer) - MSDN for more information.
The official W3C spec can be found here: High Resolution Time API
Answer from rink.attendant.6 on Stack Overflowjavascript - performance.now() vs Date.now() - Stack Overflow
Getting current date and time in JavaScript - Stack Overflow
javascript - date = new Date(); date.valueOf() vs Date.now() - Stack Overflow
JavaScript Date(Date)
Videos
ยป npm install date-fns
They both serve different purposes.
performance.now() is relative to page load and more precise in orders of magnitude. Use cases include benchmarking and other cases where a high-resolution time is required such as media (gaming, audio, video, etc.)
It should be noted that performance.now() is available in IE10 or later browsers. See caniuse page.
Date.now() is relative to the Unix epoch (1970-01-01T00:00:00Z) and dependent on system clock. Use cases include same old date manipulation ever since the beginning of JavaScript.
See When milliseconds are not enough: performance.now and now method (Internet Explorer) - MSDN for more information.
The official W3C spec can be found here: High Resolution Time API
Date.now() returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, performance.now() returns the number of milliseconds, with microseconds in the fractional part, from performance.timing.navigationStart, the start of navigation of the document, to the performance.now() call. Another important difference between Date.now() and performance.now() is that the latter is monotonically increasing, so the difference between two calls will never be negative.
For better understanding visit the link.
.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 theDateobject which will return an integer representing the current day of the week (0-6)0 == Sundayetc
so 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.
There are several ways to get the current time in JavaScript:
new Date()creates aDateobject representing current date/timenew Date().valueOf()returns number of milliseconds since midnight 01 January, 1970 UTCnew Date().getTime()Functionally equivalent tonew Date().valueOf()Date.now()Functionally equivalent to the 2 methods above
As mentioned in the comments and MDN links, Date.now() is not supported in Internet Explorer 8. So if IE 8 compatibility is a concern you should use new Date().valueOf() at the cost of slightly diminished code readability.
Or if you want to use Date.now() but must be compatible with browsers which don't support it, you can place following code somewhere in your JavaScript files, which will add support for it.
if (!Date.now) {
Date.now = function() {
return new Date().getTime();
}
}
2019 answer
Since we no longer care about IE8, only two methods are interesting:
new Date()- creates aDateobject representing the current date/timeDate.now()- returns the number of milliseconds since midnight 01 January, 1970 UTC
(The other two methods mentioned in the older answers are functionally equivalent to Date.now() but look uglier and work in IE8.)
As a matter of style, I found it clearer to use Date.now() in code that deals with intervals (usually subtracting an earlier date from a later date to calculate the time elapsed), and new Date() for timestamps, e.g. those written to a database.