noobie-php
noobie-php

Reputation: 7233

moment.js coverts date to different format

So i am trying to convert a mm/dd/yyyy format, but i am getting this as output 00/Tu/yyyy, the date object itself works fine if i print it on console.

$('#startDate').on('change', function () {
        var start_date = $("#startDate").val();

        // var datePart = start_date.split(/[^0-9]+/);
        var today = moment(new Date(start_date)).format("mm/dd/yyyy");
        console.log(today);
        $("#endDate").val(start_date);
    });

Upvotes: 0

Views: 1854

Answers (2)

VincenzoC
VincenzoC

Reputation: 31482

Moment format tokens are case sensitive so you have to use MM/DD/YYYY instead of mm/dd/yyyy.

Uppercase MM stands for month (01 02 ... 11 12), while lowercase mm stands for minutes (00 01 ... 58 59); uppercase DD stands for day of the month (01 02 ... 30 31), while lowercase dd stands for day of the week (Su Mo ... Fr Sa). There is no lowercase yyyy token but you have to use uppercase YYYY for year.

See moment format() docs for further reference.

You are getting 00/Tu/yyyy because your input represent a moment with 0 minutes of a Tuesday.

Moreover note that you do not need to pass a JS Date object to moment (new Date(start_date)), but you can use moment parsing functions.

Upvotes: 1

Akrion
Akrion

Reputation: 18515

Check momentJS docs for all the formatting options: https://momentjs.com/docs/#/displaying/

'L' or 'MM/DD/YYYY' should work fine.

Upvotes: 1

Related Questions