Rodney Wormsbecher
Rodney Wormsbecher

Reputation: 927

moment.js cannot parse date correctly

Thanks for reading, I got a date that get's returned from sharepoint as '10/2/2000 12:00:00 AM' This didn't work with moment.js and locales so I reparsed it to a ISOString, now the data '2000-10-01T22:00:00.000Z' as specified as in: https://github.com/moment/moment/issues/1407 . However it will still fallback to the standard javascript Date function and I have no idea why.

Below is my code implementation:

Code for parsing in momentJs

function dayAndFullMonth(dateTime, timezone) {
    return moment.tz(dateTime, timezone).format('DD MMM');
}

The function responsible for rewriting the date

function rewriteDates()
{
    var endPartArray = 0;
    for(var i = 0; i< vm.birthdays.length; i++)
    {
        var birthday = sharepointservice.getProperty(vm.birthdays[i].Cells, 'Birthday');
        console.log(birthday);
        var cleanBirthday = new Date(birthday.split(' ')[0]).toISOString();
        console.log(cleanBirthday);

        var dutchDates = dateservice.dayAndFullMonth(cleanBirthday, "Europe/Amsterdam");
        console.log(dutchDates);
    }
}

my console:

enter image description here

Any help would be appreciated.

Upvotes: 0

Views: 410

Answers (1)

Daniel Gary
Daniel Gary

Reputation: 507

Not sure why you're calling moment.tz, but it should just be moment('10/2/2000 12:00:00 AM'). That will parse correctly and allow you to call format.

After Edit

You need to include moment-timezone, not just moment. I've tested your code here: https://npm.runkit.com/moment-timezone and it works fine.

Upvotes: 1

Related Questions