Before asking this question I have searched all stackoverflow and read the docs but I can't just understand how to convert one UTC date to local time of an user and display it in his format. I keep trying different methods but I keep getting the same time again and again.
so my django returns obj.created_on in UTC as - 2013-12-26T13:52:24 - no timezone Info here but I know its UTC
Now I want momentjs to auto detect the user's timezone and convert it in that timezone.
Can I have proper syntax for the same?
I was trying this as well:
new_date = moment(obj.created_on).utc().local().format()
Moment has two different utc functions:
One is on a moment instance, used to switch the mode to UTC.
moment(input).utc()
The other is on the moment prototype, used to interpret the input as already in UTC.
moment.utc(input)
So, you want this syntax:
moment.utc(obj.created_on).local().format()
moment($('#start').val()).utc().format("ddd, DD MMMM YYYY H:mm:ss");
moment($('#end').val()).utc().format("ddd, DD MMMM YYYY H:mm:ss");
will display Date format in UTC if you want the date in local format.
moment($('#start').val()).utc().local().format("ddd, DD MMMM YYYY H:mm:ss");
moment($('#end').val()).utc().local().format("ddd, DD MMMM YYYY H:mm:ss");
Default will take as GMT time. We need to convert that into our local format.
Related
I'm trying to convert a time in the format of '2019-07-15T08:57:58.749081' to a local time using the format of 'Month Day(th/st) Year Hour:mm am/pm". So anything like "September 9th 2018 9:40 pm" or "July 18th 2019 9:40 pm", etc.
This is to use the moment package imported into a ReactJS app. I can get the format to look right but the time is still GMT/UTC. To do this format I used
var dateTime = moment(param).format('MMMM Do YYYY h:mm a');
But I really need the formatted time in my own local time.
Since the input string is in ISO 8601 format, and the time basis is UTC, the string should contain a trailing Z. In other words, it should look like 2019-07-15T08:57:58.749081Z. Since it doesn't, you have two choices.
You can append the Z yourself:
moment(param + 'Z').format('MMMM Do YYYY h:mm a')
You can parse as UTC and then switch to local mode before formatting:
moment.utc(param).local().format('MMMM Do YYYY h:mm a')
I want to display a unix timestamp (which is in UTC) as a formatted date.
I also want to keep it in the UTC timezone (so no change to the hours based on TZ). How can I do this?
Example:
moment(unix_timestamp).format('MMMM D, YYYY - H:mm:SS.SSSS') converts the output into the computer's local time.
Moment Docs:
Found this section in the docs, but none of the options seem to work:
https://momentjs.com/guides/#/parsing/local-utc-zone/
As far as documentation states:
Note that if you use moment() or moment.utc() to parse a date with a specified offset, the date will be converted from that offset to either local or UTC:
moment('2016-01-01T00:00:00+02:00').format() //converted to local
moment.utc('2016-01-01T00:00:00+02:00').format() //treated as UTC
I am very confused about how mongoDB and javascript handle dates/times.
I have many dates given in a specified timezone. I need to store them in ISO format in mongoDB. For example:
Given: "01.01.2013 15:00", this is in New York City's local time.
As my machine is not set to NYC's timezone, I created a Date object form this with a given offset of -04:00, as NYC is 4 hours behind UTC. So:
var date = new Date("01-01-2013T15:00-04:00"). If I open mongo-express to see what it actually stored, it shows ISODate("2013-01-01T14:00:00.000Z").
Then using moment.js to get it in NYC's time:
var test = moment("2013-01-01T14:00:00.000Z");
var out = test.tz('America/New_York').format('MMMM Do YYYY, h:mm:ss a');
console.log(out); //January 1st 2013, 9:00:00 am
This obviously shows the wrong time, it should show 3:00:00 pm. What am I doing wrong here? I need these times to be the same for any client connecting to my application from any timezone, they shouldn't be converted. What is the best practice here?
If you know that "01.01.2013 15:00" is in "America/New_York" timezone, you can parse it using moment-timezone's moment.tz method.
As moment docs states:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
Here a live example:
var m = moment.tz("01.01.2013 15:00", "DD.MM.YYYY HH:mm", "America/New_York");
console.log(m.format('MMMM Do YYYY, h:mm:ss a'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"></script>
I want to convert given time to local time. I'm using moment.js
I checked their momentjs documentation but didn't help.
I've a date & time which is stored as history.
Suppose give date & time: 2017-03-08T05:04:53.3715179.
I want to convert it to local time.
I tried doing this:
moment($(this).find('logTime').text()).utc().format('MMMM Do YYYY, h:mm');
But didn't help.
I want to convert this date & time to local date & time.
How do i do that?
What do you mean by local time ? if you are talking about your timezone then just apply the timezone.
if you want UTC time then use utc() method from moment
moment('2017-03-08T05:04:53.3715179').utc().format('MMMM Do YYYY, h:mm');
with the timezone
moment('2017-03-08T05:04:53.3715179').tz(yourTimezone).format('MMMM Do YYYY, h:mm');
example // output - "March 7th 2017, 11:34"
moment('2017-03-08T05:04:53.3715179').tz('Europe/London').format('MMMM Do YYYY, h:mm');
I want to display a GMT date time string to IST(GMT +0530) time format by using moment.js. I am assigning the PHP date time value to a javascript variable and then converting the GMT time to IST time using moment.tz().format(); method. But when I alert the converted value by specifying the format parameters ,the alert is showing the formatted time with chinese letters. moment.js is clearly confusing. Please help me solve this...
GMT Datetime value is "2014-11-28 20:15:26" which is i am getting from PHP variable
"2014-11-29T01:45:26+05:30" is value of Converted value of date time variable to IST by using moment.tz(m,zone).format();
"11月 29日 2014 1:45 早上" is the value of converted value with format parameters moment.tz(m,zone).format('MMM Do YYYY h:mm a');
My code is
var start_dates = '<?php echo $times_start; ?>';
var zone = "Asia/Kolkata";
var m = moment.tz(start_dates,'Europe/London').format();
var time = moment.tz(m,zone).format('MMM Do YYYY h:mm a');
The only way you could get Chinese characters is if you have set the Chinese locale with either the lang or locale functions. You might have done that somewhere else in your script.
With regard to the code you wrote, that interprets the input as the time in London - which is not the same thing as GMT or UTC. (London alternates between GMT and BST for daylight saving time.)
You also don't need to format it just to parse it again.
You just need to do this:
var m = moment.utc(start_dates);
var time = m.tz(zone).format('MMM Do YYYY h:mm a');
This will work for any of the supported zones. But if you know for a fact that you will always be converting to Indian Standard Time, since it doesn't use DST, you don't really need moment-timezone. You can just do this:
var m = moment.utc(start_dates);
var time = m.utcOffset("+05:30").format('MMM Do YYYY h:mm a');