Moment not working properly

#1

Why I am getting Invalid date in the following case:

function formatUnixDateString(value_) {

    //https://stackoverflow.com/questions/20943089/how-to-convert-unix-timestamp-to-calendar-date-moment-js

                      if(moment(value_).isValid()){
                       //return moment.unix(value_).format('MM/DD/YYYY');

                        return moment(value_).format('L');

                       }
                       else {
                        return "";
                       }
}

Trying this line return moment(value_).format('L'); and it keeps on printing Invalid date

My dates are in this format 1522144800000

0 Likes

#2

Unix timestamps need to be parsed using moment.unix(). Cf. https://momentjs.com/docs/#/parsing/

0 Likes

#3

Actually, this line return moment.unix(value_).format('MM/DD/YYYY'); was also giving me Invalid Date for some reason.

0 Likes

#4

works for me …

moment(1522144800000).format('MM/DD/YYYY') // "03/27/2018"
0 Likes

#5

But here you aren’t using moment.unix()?

0 Likes

#6

because that’s the millisecond-timestamp.

0 Likes