Welcome to WebmasterWorld Guest from 54.196.243.192

Forum Moderators: open

Message Too Old, No Replies

JavaScript and UTC

How to recognise summer time clock change

     
8:26 pm on May 20, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2004
posts: 81
votes: 0


I use UTC with a start and finish time and duration values and I use JavaScript to check if there are any mistakes when submitting these times eg. UTC 00:30 to UTC 03:00 = 2.5 hours, but when the clocks change then this time could be either 1.5 or 3.5 hours.

How can I get JavaScript to use UTC and not local time and recognise that the time difference is 2.5 hours?

Thanks
10:30 pm on May 20, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:943
votes: 0


There are some date object methods which refer to UTC:

Shameless copy / paste from [w3schools.com...]

getUTCDate() Returns the day of the month, according to universal time (from 1-31)
getUTCDay() Returns the day of the week, according to universal time (from 0-6)
getUTCFullYear() Returns the year, according to universal time (four digits)
getUTCHours() Returns the hour, according to universal time (from 0-23)
getUTCMilliseconds() Returns the milliseconds, according to universal time (from 0-999)
getUTCMinutes() Returns the minutes, according to universal time (from 0-59)
getUTCMonth() Returns the month, according to universal time (from 0-11)
getUTCSeconds() Returns the seconds, according to universal time (from 0-59)
setUTCDate() Sets the day of the month of a date object, according to universal time
setUTCFullYear() Sets the year of a date object, according to universal time (four digits)
setUTCHours() Sets the hour of a date object, according to universal time
setUTCMilliseconds() Sets the milliseconds of a date object, according to universal time
setUTCMinutes() Set the minutes of a date object, according to universal time
setUTCMonth() Sets the month of a date object, according to universal time
setUTCSeconds() Set the seconds of a date object, according to universal time
toUTCString() Converts a Date object to a string, according to universal time
UTC() Returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time


Example usage:
var dateObj = new Date();
alert(dateObj.getUTCHours() + ':' + dateObj.getUTCMinutes());


Obligatory: [w3fools.com...]
8:36 am on May 28, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


I use UTC ... but when the clocks change then this time could be either 1.5 or 3.5 hours.


If the time changes when the clocks change then you're not using UTC ;)

However, I'm curious how you are doing your date arithmetic if you are not using UTC? getTime() (and valueOf()) methods return the UTC timestamp (milliseconds), so you might well already be using UTC time correctly?
4:52 pm on May 28, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2004
posts: 81
votes: 0


If the time changes when the clocks change then you're not using UTC
I agree, hence my question.

The dates are taken from two text input boxes in the format of '28 May 2014 17:36 (UTC)' I need to check the difference between the two date & times based on UTC. The PHP side is working perfectly, it's just my javascript.

I didnt reply earlier as I have time to fix this before the next time change and other priorities arrived.

Thanks
10:46 pm on May 28, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


format of '28 May 2014 17:36 (UTC)'


Unless you manually parse the date string, the parentheses around the timezone identifier will prevent JavaScript from recognising the timezone and will default to local time. I think the best approach is probably just to strip the parentheses in the string before processing, then jump straight in...

// 28 May 2014 17:36 (UTC) - NB: Remove parentheses around timezone identifier 
var date1 = new Date('28 May 2014 17:36 UTC');
var date2 = new Date('28 May 2014 20:06 UTC');
// Calc difference and convert milliseconds to hours (2.5)
var diffHours = (date2 - date1) / 1000 / 60 / 60;
3:24 pm on May 29, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2004
posts: 81
votes: 0


As you mentioned, I think it may be the parenthases that is the problem. I will look at it tomorow.

Thank you.