You can use new Date ().getTime () for getting timestamps. Then you can calculate the difference between end and start and finally transform the timestamp which is ms into s. const start = new Date ().getTime (); const end = new Date ().getTime (); const diff = end - start; const seconds = Math.floor (diff / 1000 % 60); Share Follow var now = new Date (); var hours = now.getHours ()* (60*60); var minutes = now.getMinutes ()*60; var seconds = now.getSeconds (); var secSinceMidnight = hours+minutes+seconds; Share Follow answered Jun 8, 2012 at 7:07 OptimusCrime 14.6k 12 57 96 @Mala do you mean this code fails if the daylight savings correction was applied the latest midnight?
var days = parseInt (diff.asDays ()); //84 var hours = parseInt (diff.asHours ()); //2039 hours, but it gives total hours in given miliseconds which is not expacted. hours = hours - days*24; // 23 hours var minutes = parseInt (diff.asMinutes ()); //122360 minutes,but it gives total minutes in given miliseconds which is not expacted. minutes =. Date.prototype.getTime () Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.) Date.prototype.getTimezoneOffset () Returns the time-zone offset in minutes for the current locale. Date.prototype.getUTCDate ()
The function to do that is Date.getTime (). Once both Dates have been converted, subtracting the later one from the earlier one returns the difference in milliseconds. The desired interval can then be determined by dividing that number by the corresponding number of milliseconds. Number of milliseconds since 1 January 1970 : getTimezoneOffset() Difference between local time and GMT in minutes: The difference, in minutes, between UTC and local time. The value is positive if the local timezone is behind UTC and negative if it is ahead. For example, a timezone of UTC+10 (Australian Eastern Standard Time) will return -600.