Jump to content

date format issue


Destramic

Recommended Posts

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

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.