Dharam
Dharam

Reputation: 479

Momentjs not printing seconds in formatted date

I am trying to convert string to specific format but its not printing the seconds correctly.see below example

const moment = require('moment')
const convertedTime = moment('Sep 19, 2019 8:24:44 AM', 'lll').format('YYYY-MM-DD hh:mm.ss')
console.log('convertedTime', convertedTime);
// output is
// date 2019-09-19 08:24.00
// why 44 (seconds) is not printing?
// expected output date 2019-09-19 08:24.44

Upvotes: 1

Views: 291

Answers (4)

joy08
joy08

Reputation: 9652

Issue is with your formatting. Add ":" instead of "." in the format structure. Also the second argument "lll" is for locale formatting, guess you can remove that too.

Also added "A" flag for formatting AM/PM

Check this sandbox: https://codesandbox.io/s/priceless-rosalind-xl212

const moment = require("moment");
const convertedTime = moment("Sep 19, 2019 8:24:44 AM").format(
  "YYYY-MM-DD hh:mm:ss A"
);
console.log("convertedTime", convertedTime);

Upvotes: 0

Dharam
Dharam

Reputation: 479

i got this working.

const convertedTime = moment('Sep 19, 2019 8:24:44 AM', 'MMM D YYYY hh:mm:ss').format('YYYY-MM-DD hh:mm:ss')
console.log('convertedTime', convertedTime);

Upvotes: 1

Todd R
Todd R

Reputation: 18516

I may be wrong, but I think the "lll" (your second argument) applies formatting, and I think the resolution is to the minute, which would explain why your seconds are "0". Try removing the "lll" and see if you have better results.

Upvotes: 1

Lucas Raza
Lucas Raza

Reputation: 549

I believe your format is off,

Try:

const convertedTime = moment('Sep 19, 2019 8:24:44 AM', 'lll').format('YYYY-MM-DD hh:mm:ss')

That should do the trick!

For reference, it says it right on the home page of https://momentjs.com/

Upvotes: 0

Related Questions