I want to convert UTC time to local Time Zone where Time Zone will be dyanamic.
The question is asked because I want my UTC date to get converted automatically using DayLightSaving Time.
DayLightSaving for Adelaide on 2019-04-25 is +4 hours and standard is +5.
This +1 hour or -1 hour should be calculated automatically.
I hava gone through all stackoverflow similar questions but could not find or relate well. so how to convert UTC date into DST date ? momemt.js can be used but plz avoid giving links in your answer. I would prefer actual function to convert dates.
Here is the actual problem,
`Current Date = 2019-04-25 --yyyy-mm-dd
TimeZone = Adelaide
MyUTCDate = 2019-04-25 8:30:00
ConvertedDSTTime = ?`
It is unclear from your question, but perhaps you are looking for something like this?
new Date('2019-04-25T08:30:00Z').toLocaleString('en-AU', {timeZone: 'Australia/Adelaide'})
//=> "25/04/2019, 6:00:00 pm"
I am working on sample react-native app.
I am using moment.js to convert time to AM and PM representations.
My data is:
{
"start_date":"2017-09-29T18:29:59.000Z",
"end_date":"2017-09-29T19:29:59.000Z"
}
When converting the start_date and end_date to 12 hour clock format (AM/PM) using moment().format('LT'), I get the wrong time i.e. 11:59 PM for start_date - 12:59 AM for end_date.
How do I get the right time and format?
The comments are correct, since you are passing 2017-09-29T18:29:59.000Z it will turn to 11:59pm under your locale.
But LT as a format will really just return the time. Try using MM-DD-YYYY hh:mm:ss a for the format string.
You can check more here at the moment docs
I also made fiddle for you to play around with.
I am using moment and moment-timezone to try and output how far away a future time is fromNow (in any timezone).
For example, if the input time was 1 day and h hours away from now, the output would be something like:
Tomorrow at hh:hh AM
Here is how I create my moment-timezone, with some example data:
determineAirTime(dateTime) {
console.log(dateTime); // May 12th 2014 8PM
dateTime =
momentTimezone.tz(dateTime, "MMM Do YYYY hA", momentTimezone.tz.guess());
console.log(dateTime) // 2014-05-12T20:00:00-04:00
}
My problem is converting the above time (2014-05-12T20:00:00-04:00), to a more readable format. Such as tomorrow at 8:00pm, where it takes into account the timezone.
I've tried fromNow() and calendar() in moment, but they only output how many days it will be.
How can I convert my moment-timezone string to an exact date/day/seconds from the currentTime?
You should be careful will "friendly" formats, many users find them annoying. Anyhow, you can use the calendar method:
// Create a date for 13:15:23 tomorrow
var d = new Date();
d.setDate(d.getDate()+1);
d.setHours(13,15,23);
// Show friendly format
console.log(moment(d).calendar());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I am having a problem with Moment.js.
Here are the codes.
var date = "2016-07-26 06:15 pm";
var unixDate = moment(date).unix();
var renderDate = moment.unix(unixDate).format("YYYY-MM-DD hh:mm a");
console.log(renderDate);
I want the value of "renderDate" to be same as the value of "date" variable. However, what gets printed out in the console is "2016-07-26 06:15 am".
I am completely lost as to why everything remains the same except "am/pm" at the end.
unix method accepts only number
You need to parse date before usage
moment('2016-07-28 06:15 PM', 'YYYY-MM-DD hh:mm a').format('YYYY/MM/DD hh:mm a')
Look at http://momentjs.com/docs/. You can only use the string without a format if it is in ISO 8601 format. In your example, it ignored the am/pm indicator and viewed the hour as being in 24-hour format.
Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven't had any success.
Is there a way using moment.js to parse an ISO-8601 string but have it parsed in my local timzeone? Essentially I want to ignore the timezone information that is supplied in the ISO string.
For example, if I am in EDT timezone:
var x = moment( "2012-12-31T00:00:00+0000" );
will give me:
"2012-12-30T19:00:00-5000"
I'm looking to ignore the timezone info and just have it give me a moment equivalent of "2012-12-31T00:00:00-5000" local time (EDT).
I don't think you really want to ignore the offset. That would ultimately just be replacing the offset you provided with one from your local time zone - and that would result in a completely different moment in time.
Perhaps you are just looking for a way to have a moment retain the time zone it was given? If so, then use the moment.parseZone function. For example:
var m = moment.parseZone("2012-12-31T00:00:00+0000");
var s = m.format(); // "2012-12-31T00:00:00+00:00"
You could also achieve this with moment.utc. The difference is that moment.parseZone will retain whatever offset you give it, while moment.utc will adjust to UTC if you give it a non-zero offset.
I solved this by supplying a format as the second argument, and using Moment's method of escaping characters, and wrapped square brackets around the timezone.
moment("2016-01-01T05:00:00-05:00", "YYYY-MM-DDTHH:mm:ss[Z]").startOf("hour").format()
This will still create moment objects using your local time zone, but it won't do any sort of auto-timezone calculation. So the above example will give you 5am regardless of timezone supplied.
I know I'm late to the party, I had the same question and my searches didn't bring me any closer. I broke down and read the documentation and there is an option in moment for a String + Format:
String + Format docs
moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);
moment(String, String, String, Boolean);
and more words, then this:
Unless you specify a time zone offset, parsing a string will create a date in the current time zone.
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
The part that gave me pause was the example that was used to parse local time omitted the +0000, which lead me to think the input string needed to have that removed, but it doesn't.
example:
var time = "2012-12-31T00:00:00+0000";
var x = moment(time); // Sun Dec 30 2012 19:00:00 GMT-0500
var y = moment(time,'YYYY-MM-DD'); //Mon Dec 31 2012 00:00:00 GMT-0500
You can ignore the browser's timezone completely by creating a new moment using moment.utc() instead of moment().
For example, if you are trying to work purely with a UTC date/time of the browser's current time but want to discard its timezone data, you can recreate the browser's current time into a UTC format using the following:
let nowWithTimezone = moment();
let nowInUtc = moment.utc(nowWithTimezone.format('MM/DD/YYYY HH:mm'), 'MM/DD/YYYY HH:mm');
Further documentation on moment.utc(): https://momentjs.com/docs/#/parsing/utc/
If you know for sure your input string is in the ISO-8601 format, you could just strip off the last 5 digits and use that in the Moment constructor.
var input = "2012-12-31T00:00:00+0000"
input = input.substring(0, input.length-5)
moment(input).toString()
> "Mon Dec 31 2012 00:00:00 GMT-0600"
There are valid reasons to do what the OP is asking for. The easiest way to do this with Moment is using its parseZone(date) method. No futzing around with string manipulation or multiple calls. It effectively parses the date string as though it were in the browser's local time zone.
This is difficult task to do with MomentJS, it will basically depend as well on your current timezone.
Documentation as well is vague for this specific task, the way I solved the issue on my side was by adding hours to the date before converting it to JSON format.
var dt = moment("Sun Sep 13 2015 00:00:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400", false);
var date = dt.add(2, 'hour').toJSON();
console.log(date); //2015-09-13T00:00:00.000Z
Momentjs default logic will format the given time with local timezone. To format original date, I wrote a function:
https://github.com/moment/moment/issues/2788#issuecomment-321950638
Use moment.parseZone to convert without taking into account the timezone.
const moment = require('moment')
const dateStr = '2020-07-21T10:00:00-09'
const date = moment.parseZone(dateStr)
console.log(date.format('MM-DD-YY HH:mm A')) // 07-21-20 10:00 AM
Try here link to docs
The best way is to use:
dt = moment("Wed Sep 16 2015 18:31:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400",true);
And to display convert again to desired timezone:
dt.utcOffset("-04:00").toString()
output > Wed Sep 16 2015 18:31:00 GMT-0400