MomentJS returning duplicate months? - javascript

I'm using MomentJS to parse a Date String that I get from a service into a String month.
The String is formatted like "2013-1" Which should be January. Moment gets this correct.
However, something is weird once I get beyond August.
I have for example
console.log(moment('2012-9').format('MMMM'));
Which returns September for me.
Then I do:
console.log(moment('2012-10').format('MMMM')); it also returns September.
This eventually maps 11 to October and 12 to December, and thus an entire data set for the month of December is cutoff.
Am I doing something wrong with moment, or what could the issue be? I've checked all the other months and it does not do this.

I've determined the reason for this happening.
The format of the date, '2012-1' is not a standard ISO date.
To return the proper month when I work with the date, I have to specify the format my date string is in, so I use the
console.log(moment('2012-10', 'YYYY-M').format('MMMM'));
Which returns the correct month, October.
Thank you everyone for all the help.

Final Edit to anyone else facing the same problem:moment('2012-10','YYYY-MM').format('MMMM'); solved the issue for OP
Which momentJS version are you using? I just tried with 2.10.5 and
console.log(moment('2012-9').format('MMMM'));//Returns September
console.log(moment('2012-10').format('MMMM'));//Returns October
Screenshot below is of the code running on the momentjs website in a console:
Upgrade to the latest version if you still face issues.

Related

The date 2019-04-01T00:00:00.000Z gives me previous month (March, not April)

So, I have the 1st day of month as 2019-04-01T00:00:00.000Z
When I use moment(date).month(), it returns me 2, not 3.
So, I receive March, not April.
What is the problem with it, and why I receive the previous month from the date? Maybe the problem in the TimeZone? Because my TimeZone is GMT-4, so, maybe this is problem?
Should I use UTC or ISO string instead to work with the date?
Like you mentioned, your timezone is GMT-4. The date that you are providing is in UTC. The 'Z' at the end stands for Zulu time, which is the same as UTC.
momentjs will convert this to local time.
How to handle this all depends on what you need the date for.
If this is a date that you saved somewhere before on a server, it might be important to add the correct timezone to it when you are saving it.
Be careful if you let a server create these dates, because the server might be running in a different timezone than your client.
If you create a new Date() in JS it will return a date object with the current time of your local time. If this happens on a server that's running in a different timezone, or in UTC (for example Docker containers), it will create a date in that timezone.
The best way to solve this is to think about your exact use case.
There are tons of articles written about handling dates and it's not easy.
If you have some time, this podcast helps to explain how dates work and will help you to get a better understanding of dates in general:
Coding Blocks - Why Date-ing Is Hard

Moment.js local time zone issue

Using the code below, I am getting a Date from my database (SQL) and displaying it in a datapicker form field. The date displays fine for me but if I change my time zone (EST) in my system to one that is behind mine, the field will display the date as the day before. Does anyone know why this is occurring and how to fix it?
var NetNewBusinessDate = moment(model.NetNewBusinessDate).format("M/D/YYYY"));
model.NetNewBusinessDate == "/Date(1494561600000)/"
Found the answer here. Moment.js local relative time
The issue had to do with UTC conversion and Moment.js has an extension method that handles that. I just used moment.utc instead of moment and it worked like a charm!

Different result with same js code on pc and tizen converting date from gregorian to persian

I have a js method that converts system's gregorian date to jalali (persian) date.
Though it's working correctly on pc and all my websites, the result on tizen watch face that uses javascript on its web layout have become invalid since july 1st. I mean the result is 1 day more than what it should be.
I know it's a leap year and the code handles it fine...
I have replaced the convert code with another sample and it's just the same.
Does anybody else have this problem or know about what the solution might be?
Is there any difference between js on pc and tizen device?
As far i know currently only Gregorian calendar is available on Tizen officially.
Source Samsung Dev
I should say that it was my fault and it was wrong function input when calling convert function. I was just sending the GetMonth() result and did not consider it was returning 0-11 for month, and it is needed for convert function to get month number as 1-12.
By just adding one to the month it is alright now.
thanks everybody.

Chrome & Firefox Date function different. (new Date)

new Date
when I use the date function in chrome I get something like this
Thu Jul 16 2015 20:37:47 GMT-0700 (Pacific Daylight Time)
when I do the same function in firefox I get this
2015-07-17T03:29:03.110Z
why are they different? I googled for a bit and search on stack but even the examples they were using to show their problem were comparing two dates of the same format. What I'd really like to know is why are they different now and how can I go about making them the chrome format cross as many browsers as possible?
Thanks
Resolved with moment.js
Firefox is in fact giving a different date string, and not simply displaying is differently as stated by Xufox.
The function I am going to use is
moment(new Date()).format("YYYY MMM D H:mm:ss")
but I will change around the .format, just wanted to get the answer here in case others run into the same problem
EDIT!!!!:
Xufox was right. I was a dummy. I had to manually edit some code in my timeline feed to get it going. I needed to use Date.parse on my generic time string, do my editing to the time, then use new Date. Thanks Xufox.

Strange javascript Date toISOString

I am using fullcalendar for a project of mine. I have a strange problem that keeps bugging me. When I export an event, I want to get it in timestamp format, which is OK. the problem is when I try to convert it to ISO format using Date.toISOString function, it gives me the time 2 hours earlier. I think it is a problem of my timezone, because I posted my timestamp on http://www.unixtimestamp.com/ and it gives me the right time, but when I do it in my browser, it gives me the date and time with 2 hours earlier. I can't seem to figure it out. I googled a lot for a solution, but nothing so far. Does anybody have a clue?
toISOString will give you a string to represent the time in UTC. If you look at the result closely enough, you can find there is a "Z" in the end, which means the timezone is UTC.

Categories

Resources