Destramic Posted October 27, 2014 Share Posted October 27, 2014 when trying to get the date var visitors_time = new Date(); i'll being back a result like Mon Oct 27 2014 20:54:55 GMT+0000 (GMT Standard Time) my issue is, is there a way of getting the end part with the brackets to not show up? ie. format the date?...ive googled but doesnt seem no simple solution like php to actually format the date to the way you'd like it thank you guys Quote Link to comment https://forums.phpfreaks.com/topic/292107-date-format-issue/ Share on other sites More sharing options...
Psycho Posted October 27, 2014 Share Posted October 27, 2014 If you really want it exactly as it is returned with the exception of the part in parenthesis, then just use a string manipulation to only get the first part. You need to convert it to a string first. var visitors_time = new Date().toString(); var displayDate = visitors_time.substr(0, visitors_time.indexOf('(')-1); Quote Link to comment https://forums.phpfreaks.com/topic/292107-date-format-issue/#findComment-1494978 Share on other sites More sharing options...
Destramic Posted October 28, 2014 Author Share Posted October 28, 2014 that worked like a dream...thanks one more thing im using var offset = new Date().getTimezoneOffset(); to get users timezone but when testing on my system its always returning 0...so im guessing i would have to compare it againts a utc timestamp to work out the difference to get the result im looking for? Quote Link to comment https://forums.phpfreaks.com/topic/292107-date-format-issue/#findComment-1495002 Share on other sites More sharing options...
Psycho Posted October 28, 2014 Share Posted October 28, 2014 Not sure I follow you. getTimezoneOffset() returns the difference between the user's timzone and UTC. You must live in a timezone the same as UTC. Are you wanting to translate that into a textual description of the timezone? E.g. PST (Pacific Standard Time)? If so, you would have to create a dictionary of all the possible values. But, it would not be 'accurate' since multiple users can have the same "time" (and UTC offset) but be in different timezones. Quote Link to comment https://forums.phpfreaks.com/topic/292107-date-format-issue/#findComment-1495016 Share on other sites More sharing options...
Destramic Posted October 28, 2014 Author Share Posted October 28, 2014 ah ok thats good to know it compares to a utc timezone...but when i change the my system time (UTC + 1...) shouldnt that change the offset to something else other than 0 IE. 1? but yes im wanting to then translate the value to +00:00, +01:00 etc.. thank you for the useful information Quote Link to comment https://forums.phpfreaks.com/topic/292107-date-format-issue/#findComment-1495023 Share on other sites More sharing options...
Destramic Posted October 28, 2014 Author Share Posted October 28, 2014 (edited) when looking i bumped into this script, which i had to alter a few things function pad(number, length) { var str = "" + number while (str.length < length) { str = '0'+str } return str } var offset = new Date().getTimezoneOffset(); offset = ((offset <0 ? '-' : '+') + pad(parseInt(Math.abs(offset/60)), 2) + ':' + pad(Math.abs(offset%60), 2)); alert(offset); seems to do the trick in return the offset i need....+00:00 Edited October 28, 2014 by Destramic Quote Link to comment https://forums.phpfreaks.com/topic/292107-date-format-issue/#findComment-1495026 Share on other sites More sharing options...
codefossa Posted October 30, 2014 Share Posted October 30, 2014 (edited) That's because getTimezoneOffset() returns the offset in minutes rather than hours. The pad() function you have there just repeats a string and adds it to the beginning of the string you give it, in this case the time. That shouldn't have changed the outcome of getTimezoneOffset() though. It should have return something different if you changed your system's timezone. For example, I get 240. One note. Your line offset = ((offset <0 ? '-' : '+') + is wrong. You want to switch the strings because 240 is GMT-4 and -120 would be GMT+2. Edited October 30, 2014 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/292107-date-format-issue/#findComment-1495242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.