Why is moment JS subtracting or adding days not working? - momentjs

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.

Related

Moment without time output date type not string

var moment = require('moment');
var c = moment().toDate();
How to output date without time in this format DD-MM-YYYY
Thanks
If you want to output the date in the format DD-MM-YYYY, simply use
var moment = require('moment);
var c = moment().format("DD-MM-YYYY");
toDate() converts the moment instance into a JS date, which it doesn't sound like you want to do.
I would advise looking at the Moment documentation for questions like this, it's very detailed and easy to understand.

MomentJS is reversing day and month in format()

I'm using momentJS to format some dates for a chartJS implementation.
In the chart i'm predicting the next 15 minutes worth of data so need to add the next 15 minutess worth of datetimes to my chartLabel variable.
I use the below function to achieve this. The issue is that the formatting for every other datetime seems to reverse the day and month. I have no idea why this is happening, has anyone seen this before?
predictMarketData(){
let lastDate = this.marketData[this.marketData.length-1][0]
lastDate = moment.unix(lastDate).add(1, 'minutes').format('DD/MM h:mm a')
for(var _i = 0; _i < 15; _i++){
this.chartLabels.push(lastDate);
lastDate = moment(lastDate).add(1, 'minutes').format('DD/MM h:mm a');
}
}
It appears to add minutes correctly but the day month formatting appears to reverse every other, screenshot provided.
Any help would be much appreciated!

Displaying local time from ISO 8601 string with Momentjs

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.

Converting string to moment gives wrong year

This is my code in js:
var x = "2017-1-2";
var y = moment(x, "yyyy-MM-DD")
And when I watch y in console this is what it is:
As you can see, the _d has the correct day and monthe, but the wrong year. Why is this happening and how to fix it? I use that value to send it to server via ajax and the wrong year gets sent.
Use uppercase YYYY instead. It is mentioned in the docs. Also don't forget that construction using moment() will treat the date as local date and then convert it into UTC. If you want to treat the date as UTC, you can use moment.utc().
In my timezone (GMT+7) the code below will show "2017-01-01T17:00:00.000Z".
var x = "2017-1-2";
var y = moment(x, "YYYY-MM-DD")
console.log(y);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>

How to convert from UTC to local time in moment.js?

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

Resources