I am working on a conversion of an UTC date.
The date format received from the server inside the JSON file has the following format:
"OpenDate":"2015-10-26T08:00:00Z"
I translate the date using the following function:
var myDate = myJson.OpenDate.toLocaleString();
The problem occurs after 25th of October, when the legal time is changed. After 25th of October my dates are 1 hour earlier, which they shouldn't. So how can I keep into consideration the change in time? If the appointment says 8 a.m. UTC it should be always 9 a.m. for our timezone, even when the legal time is changed.
Related
I came across this date string:
2017-08-19T11:54:00MESZ
When I attempt to convert it as follows, it returns NaN:
let date = Date.parse("2017-08-19T11:54:00MESZ");
It appears that the ending on this string isn't recognized.
The format you're using is incorrect.
Take a look at this:
(new Date).toISOString()
"2017-08-19T10:05:18.700Z"
In contrast, the string you're trying to convert is:
"2017-08-19T11:54:00.MESZ"
Date.parse works with a variant of ISO8601 or RFC2822. Let's see how both options would work for you.
Working with ISO
It would accept either a UTC time zone descriptor ("Z") or a time zone offset (read on). Therefore the timezone instruction "MESZ" is invalid.
To reach a valid solution, you'd have to calculate the time zone offset from MESZ to Z, which is +2 hours, and append the the time expressed using wanted time zone:
Date.parse("2017-08-19T11:54:00+02:00")
1503136440000
Working with RFC2822
Example: "Mon, 25 Dec 1995 13:30:00 GMT"
Problem with RFC is that it does not accept any other time zone than GMT, UTC and Pacific, Central, Eastern, Mountain times which is not really useful for you right now. You're left off with the same problem: you'd have to express the date with a time zone offset instruction:
Date.parse("Sat, 19 Aug 2017 11:54:00 +0200")
1503136440000
You're basically stuck with UTC, but frankly it's probably a good thing in many ways.
I have a server returning a time value like so
14:00
The reason for this is it represents the start time of say a task that happens repetively say every monday at 14:00 so the date is irrelevant. I want to display this value to the user when they are editing the task so that they can change it with ui-bootstraps timepicker.
The timepicker requires either an epoch, an rfc2822 or ISO 8601 date so I need to just add a random date that I can discard later for the timepicker to be able parse and display the value (which seems ridiculous seen as it is a time picker not a time on a specific day picker but whatever)
so either using the javascript date object or moment.js how can I take that value and create a valid date object with it?
As you requested a moment.js solution also:
var date = moment("14:00", "HH:mm").toDate();
// assume returned is the hours and minutes you get in this format: 14:00
hourData = returned.split(':');
var date = new Date();
date.setHours(hourData[0]);
date.setMinutes(hourData[1]);
date will be something like Sat Jul 12 2014 14:00:13 GMT+0300 (EEST)
I've been looking for days to find how to get moment.js to behave correctly and return the correct date for a specific local time zone.
Here is my challenge:
I'm calling a flight api to get the "arrival date/time" of a flight. It provides me the arrival time in epoch time and a timezone for the airport.
I'm using javascript moment.js to convert that to the local time of the airport, BUT, the time always comes in a couple days ahead.
Here's my code:
var dateVal = 1395184260;
var day = moment.unix(dateVal).tz('America/Vancouver').format();
console.log("tz :",day);
// should return: 4:21 PM - Sun Mar-16-2014 BUT it always returns the 18th instead of the 16th.
Where are you getting the "should return" from?
According to http://www.epochconverter.com/epoch/timezones.php?epoch=1395184260, your time should be
Mar 18 2014 16:11:00 GMT-7:00
This fiddle using your timestamp:
var dateVal = 1395184260;
var date = moment.unix(dateVal);
console.log(date.tz("America/Vancouver").format('ll HH:mm:ss Z'))
returns:
Mar 18 2014 16:11:00 -07:00
I'd check whatever converter you're using to see if there's a bug.
I'm trying to parse the value of a datetime-local input so the format is 'yyyy-mm-ddTHH:mm'. I want the assumption to be that I'm parsing in the local time zone of the user, i.e. if they enter 09:00 they mean 9am in their time zone.
Also not just the current time offset, e.g. if the user enters a date in June then they mean daylight savings time regardless of the fact it's now November and DST has ended.
I've tried using regular Date objects and moment.js but the assumption is always that the time zone is UTC.
Is there a way to do this?
// dateString is the value from datetime-local
var dateInLocal = moment(dateString, "YYYY-MM-DDThh:mm");
I have a problem converting a timestamp in javascript.
I have this timestamp:
2011-10-26T12:00:00-04:00
I have been trying to format it to be readable. So far, it converts this using the local time of my system instead of the GMT offset in the timestamp. I know the timezone that this was created in is EST. I'm in PST, so the times are being offset by 3 hours.
Instead of this showing as:
Wednesday October 26, 2011 12:00 pm
It shows as:
Wednesday October 26, 2011 9:00 am
I have tried a few different solutions, but the latest one is found here: http://blog.stevenlevithan.com/archives/date-time-format
I am less concerned with the formatting part as I am with figuring out how to handle the GMT offsets. Would appreciate any help and insight anyone can provide.
Date objects are created in the local zone. If the date string was created in a different time zone, then you need to adjust the date object to allow for the difference.
The abbreviations PST and EST are ambiguous on the web, there is no standard for time zone abbreviations and some represent two or zones. You should express your zone only in terms of +/- UTC or GMT (which are the same thing, more or less).
You can get the local time zone offset using Date.prototype.getTimezoneOffset, which returns the offset in minutes that must be added to a local time to get UTC. Calculate the offset for where the time string was created and apply it to the created date object (simply add or subtract the difference in minutes as appropriate).
If your time zone is -3hrs, getTimezoneOffset will return +180 for a date object created in that zone. If the string is from a zone -4hrs, its offset is +240. So you can do:
var localDate = new Date('2011-10-26T12:00:00') // date from string;
var originOffset = 240;
var localOffset = localDate.getTimezoneOffset();
localDate.setMinutes( localDate.getMinutes() + originOffset - localOffset );
Adding the origin offset sets it to UTC, subracting the local offset sets it to local time.
It would be much easier if the time string that was sent by the server was in UTC, that way you just apply the local offset.
Edit
IE will not parse a time string with an offset, and Chrome thinks that the above time string is UTC and adjusts for local offset. So don't let Date parse the string, do it manually.
It doesn't matter what time zone you are- the time stamp will result in a different local time for every different time-zone, but they all will be correct, and anyone checking the UTC time of the date will get the same time-stamp:
new Date('2011-10-26T12:00:00-04:00').toUTCString()
returns
Wed, 26 Oct 2011 16:00:00 GMT
and getTime() anywhere returns the same milliseconds universal timestamp:1319644800000