7

I receive a date in a string format with an offset, but javascript is converting it to local device time

var d = new Date("2012-11-13T11:34:58-05:00");
debug.log(d);

returns Tue Nov 13 2012 17:34:58 GMT+0100 (CET)

var offset = d.getTimezoneOffset();
debug.log(offset);

returns -60 (my device is utc +1h)

I just want to have the time with the offset, or having the timezone offset mentioned in the string (-5h in the example)

1
  • With that, what if I also want to get the timezone abbreviation (IST, EST) after extracting the offset. Any library for this? Commented Jan 4, 2019 at 10:09

2 Answers 2

2

Well the only solution I've found is to create my custom time object by parsing the string

//ex: 2012-11-13T10:56:58-05:00
function CustomDate(timeString){

    var completeDate = timeString.split("T")[0];
    var timeAndOffset = timeString.split("T")[1];
    //date
    this.year = completeDate.split("-")[0];
    this.month = completeDate.split("-")[1];
    this.day = completeDate.split("-")[2];

    this.date = this.year + "/" + this.month + "/"+this.day;

    //negative time offset
    if (timeAndOffset.search("-") != -1){
        var completeOffset = timeAndOffset.split("-")[1];
        this.offset = parseInt(completeOffset.split(":")[0]) * -1;

        var originalTime = timeAndOffset.split("-")[0];
        this.hours = parseInt(originalTime.split(":")[0]);
        this.minutes = parseInt(originalTime.split(":")[1]);
        this.seconds = parseInt(originalTime.split(":")[2]);

        this.time = this.hours + ":" + this.minutes + ":"+this.seconds;

    }
    ///positive time offset
    else if (timeAndOffset.search(/\+/) != -1){

        var completeOffset = timeAndOffset.split("+")[1];
        this.offset = parseInt(completeOffset.split(":")[0]);

        var originalTime = timeAndOffset.split("+")[0];
        this.hours = parseInt( originalTime.split(":")[0]);
        this.minutes = parseInt(originalTime.split(":")[1]);
        this.seconds = parseInt(originalTime.split(":")[2]);

        this.time = this.hours + ":" + this.minutes + ":"+this.seconds;
    }
    //no time offset declared
    else{
        this.hours = parseInt(timeAndOffset.split(":")[0]);
        this.minutes = parseInt(timeAndOffset.split(":")[1]);
        this.seconds = parseInt(timeAndOffset.split(":")[2]);
        this.offset = 0;

        this.time = this.hours + ":" + this.minutes + ":"+this.seconds;
    }
}

For example if I want to display what is the received time 2012-11-13T11:34:58-05:00 in the specified timezone offset :

var aDate = new CustomDate("2012-11-13T11:34:58-05:00");
alert("date: " + aDate.date +" time: "+aDate.time+" offset: "+aDate.offset);

and I get

date: 2012/11/13 time: 11:34:58 offset: -5

The problem with this solution is that the date and time conventions are defined manually in the code, so they won't be automatically adapted to the user's language.

0

I don't think that JavaScript can provide an offset other then the local one as it doesn't know where the time you gave it came from..

So you've given it 2012-11-13T11:34:58-05:00 but no information about where this time originate, it could be the time in anywhere in the world, so it defaults to whatever your local timezone if for the offset.

3
  • I think it's the minus 5 at the end of the time (-05:00) that indicates the timezone offset In the example i'm saying it is 11:34am in NY time.
    – Omaty
    Commented Nov 14, 2012 at 6:44
  • Ok well you might be right, Im not sure. Well you know that -60 is -60 minutes right? So I guess you could convert it back into hours -60/1 = -1 and then add it to the string.
    – Aaron Cole
    Commented Nov 14, 2012 at 7:41
  • Yes I know that -60 is -60 minutes but that is too late, this indicate the device time offset and not the offset of the string. The problem is that I receive the string from a server with an offset that can be different from a string to another, I just want to read the time in the indicated timezone in the string.
    – Omaty
    Commented Nov 14, 2012 at 8:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.