homepage Welcome to WebmasterWorld Guest from 54.196.168.78
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JavaScript and UTC
How to recognise summer time clock change
Wayder




msg:4672794
 8:26 pm on May 20, 2014 (gmt 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

 

Readie




msg:4672831
 10:30 pm on May 20, 2014 (gmt 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...]

penders




msg:4675343
 8:36 am on May 28, 2014 (gmt 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?

Wayder




msg:4675454
 4:52 pm on May 28, 2014 (gmt 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

penders




msg:4675526
 10:46 pm on May 28, 2014 (gmt 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;

Wayder




msg:4675826
 3:24 pm on May 29, 2014 (gmt 0)

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

Thank you.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved