I have a simple use case but I cannot figure out how to convert from GMT/UTC to local time in moment.js.
Example:
var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH").format('YYYY-MMM-DD h:mm A');
console.log(gmtDateTime) emits 2015-Oct-24 8:00 PM, which is correct. Now I simply want to convert this to my local time, which happens to be Mountain Daylight Time. So the correct converted date would be 2015-Oct-24 2:00 PM, because I am 6 hours earlier than GMT/UTC.
How can this be done simply with moment.js?
Try moment().local().
Example:
var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH")
var local = gmtDateTime.local().format('YYYY-MMM-DD h:mm A');
However, #antti-kuosmanen answer is accepted and correct.
moment().local() is a correct way. But this can do in a single line. Here is how
var local = moment.utc("2015-10-24 20:00").local().format('YYYY-MMM-DD h:mm A');
No need to formating twice for the results
Cheers!! Read Simple Write Simple
Related
I'm trying to use Moment JS and having issues adding or subtracting dates.
For example, I'm trying to sanity check right now and have this in code:
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = startMoment.subtract(1, "day");
^ this doesn't work and when I print future and start moment, they show the same time and date.
I tried also changing stuff around so that I create a new moment instead of using startMoment:
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A").subtract(1, "day");
But it's still not working. Am I just missing something?
Help would be greatly appreciated as I am new to this and have wracking my head for a while on why it's not working.
Watch the documentation closely, substracting or adding dates forces you to specify the chrono unit. You just wrote 'day', but its either 'days' or just simply 'd'.
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = startMoment.subtract(1, "days");
will work.
I have a complicated problem with time converting;
I am coding by node.js and use moment, moment-jalaali and moment-timezone
I get a time from clinet in jalali format( example: 1396-03-03T23:00:00.00+04:30) to search and find some data before or after a time which is saved on UTC; My server has -04:00 zone;
var moment = require('moment');
var momentJalali = require('moment-jalaali');
var momentTZ = require('moment-timezone');
var jFormat = "jYYYY-jM-jD HH:mm:ss z";
var format = "YYYY-M-D HH:mm:ss z";
var toDate = momentJalali(req.body.toDate, jFormat).tz('UTC').format(format);
console.log("date: \n" + toDate "\n " + moment().format('Z') + "\n"); //output: date:
2017-5-25 03:00:00 UTC
-04:00
The response I expect is 2017-5-24 19:30:00 UTC; How could I reach that?
You do not need moment-timezone for this. Simply parse the input in UTC mode. Since you provided an offset, it will be taken into account automatically.
moment.utc("1396-03-03T23:00:00.00+04:30", "jYYYY-jMM-jDDTHH:mm:ss.SSZ")
.format("YYYY-MM-DD HH:mm:ss z")
//=> "2017-05-24 18:30:00 UTC"
Also note you had a few formatting tokens wrong - they are case-sensitive.
Additionally, I would seriously consider not using that particular input format if you have any control over it. By convention, it appears to be in ISO-8601 extended format, except that ISO-8601 is strictly bound to the proleptic Gregorian calendar. My understanding is that Jalaali dates are typically written as 1396/3/3 23:00:00, which would be jYYYY/jM/jD HH:mm:ss.
Also note that the value you asked for in your question is actually an hour off. your local time is 4.5 hours ahead of UTC so subtract: 23 - 4.5 = 18.5, thus 18:30 UTC, not 19:30 UTC.
I want to convert the time from the standard 12-hour reading to the 24-hour one in simpleWeather. When I try to do it, it returns the time in the Unix epoch format.
$(function sunset(){
var sunset=
$.simpleWeather({
location:'Dalol,Afar',
woeid:'',
units:'f',
success:function(weather){
var Sunset= moment(weather.sunset, "HH:mm:ss");
html='Sunset: '+Sunset;
$('#sunset').html(html);
}
});
});
I had contacted the developer on how to do convert it using moment.js, but he didn't say much more than that. How can the code be corrected to display the desired format?
You need to first parse and then format the weather.sunset with the right formatting:
var sunset = moment(weather.sunset, ["h:mm A"]).format("HH:mm");
simpleWeather.js returns the sunset as "5:31 pm" so the corresponding moment format to parse this date is h:mm A, where the A captures the pm/am. To format it in the 24-hour notation, format("HH:mm") will do the job.
I want to display the local time from an ISO 8601 string using momentjs.
There is a discrepancy of minutes when I convert an ISO string using different date formats. If I use 'MM/DD/YYYY HH:mm', the minutes is correctly displayed. If I use 'ddd, MMM Do HH:MMa', 11 minutes is added (in my case).
My sample js (babel) code:
let today = moment('11/09/2016 00:00', 'MM/DD/YYYY HH:mm').toISOString();
//today = 2016-11-09T08:00:00.000Z
let formatted = moment(today, moment.ISO_8601).format('MM/DD/YYYY HH:mm');
//formatted = 11/09/2016 00:00
let formatted2 = moment(today, moment.ISO_8601).format('ddd, MMM Do HH:MMa');
//formatted2 = Wed, Nov 9th 00:11am
I would prefer using the second format. Can someone explain why there is a discrepancy?
Please see this fiddle: https://jsfiddle.net/anudhagat/8fgtjbc7/3/
I caught my silly mistake. I have capitalized the minutes in the second format, using MM makes it display months instead of minutes.
My task is to use a datepicker to pick a date in the prescribed format, eg(MM-DD-YYYY) and pass it to the server as ISO formatted.
While it test the output the ISO formatted date is one day behind.
For example
If i select
07-13-2015
My Output ISO format is
ISO format is :2015-07-12T18:30:00.000Z
Here you can see date is 13 but the output date is 12
I am from India. I tried with zone and utcOffset, ended up with no results. How do i set it right
Here is the JSFIDDLE
js code
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(moment(selectedDate).toISOString());
});
I do have a hidden field which value will be updated on change and that will be processed in the server. No issues on that.
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(selectedDate.toISOString());
});
Your selectedDate is already a moment object so you do not need to feed it back into another moment.
Example:
var test = '07-13-2015'
var mtest = moment(test,"MM-DD-YYYY")
mtest.toISOString()
"2015-07-13T06:00:00.000Z"
Your could try converting the date format to UTC at once.
selectedDate = moment(selectedDate).utc('MM-DD-YYYY')
According to http://dygraphs.com/date-formats.html, if you pass a string like '07-13-2015', it means Midnight of 13th July 2015. Now, if you use toISOString function, it will convert it to UTC by default. To not convert it to UTC, just pass a parameter true in the toISOString function. (Moment.js docs)
For example:
var date = '07-13-2015';
date = moment(date,'MM-DD-YYY');
console.log(date.toISOString(true));
This way, moment will not convert the date to UTC.