Hunt
Hunt

Reputation: 8425

convert milliseconds to AM and PM date format

I am storing TIMESTAMP in database and when i fetch it back from database i want to convert it into AM and PM date format.

 var dbDate = moment(milliseconds); // **i am getting an error over here** 
 var data = dbDate.format("hh:mm:A").split(":");

but i am getting following error moment(milliseconds);

"Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.

Upvotes: 0

Views: 1464

Answers (2)

James Thorpe
James Thorpe

Reputation: 32202

The moment library, by default, only supports a limited number of formats within the constructor. If you don't use one of those formats, it defaults back to using new Date, where browsers are free to interpret the given date how they choose unless it fits certain criteria.

The deprecation warning is there to warn you of this behaviour - it's not necessarily an error.

In your case, you have milliseconds, so you can use the moment constructor that has a format parameter, telling it you're specifically passing in milliseconds:

var dbDate = moment(milliseconds, 'x');

All this assumes that you currently have milliseconds being returned to your JavaScript layer as a String. If you return it and treat it as a Number, you shouldn't be seeing that warning, as moment also has a specific constructor that takes a single Number, which if your milliseconds parameter is a Number should be being used already.

Upvotes: 1

Doug McLean
Doug McLean

Reputation: 1309

I'm not all that familiar with moment.js but from a quick look in the docs I don't think the constructor supports a milliseconds argument. Try this:

var dbDate = moment({milliseconds: milliseconds});

edit

The above code might just treat that as the value after the decimal point in the seconds - if so try this:

var dbDate = moment(new Date(milliseconds));

Upvotes: 0

Related Questions