NewUser
NewUser

Reputation: 13323

Get custom date time format in moment.js

I have time saved in database like this 2016-01-30 14:36:00

But I am using moment.js and I want to convert the time as in this format

2016-01-30T14:36:00

So to get that format I tried like this

var date = new Date();
console.log(moment(date).parseZone().format());

but its showing date like this 2015-12-21T15:20:04+05:30

So can someone tell me how to get the time in that format in moment.js

Upvotes: 1

Views: 1188

Answers (4)

Zumry Mohamed
Zumry Mohamed

Reputation: 9558

This will help you

var currentDate = moment(new Date()).format('YYYY-MM-DDTHH:mm:ss');
 //2015-12-21T15:50:28

Upvotes: 0

Shanoor
Shanoor

Reputation: 13692

Use a custom format:

var date = moment();
var out = date.format('YYYY-MM-DDTHH:mm:ss');

console.log(out);

Upvotes: 0

DfrDkn
DfrDkn

Reputation: 1380

Try using the below code -

console.log(moment(new Date()).format('YYYY-MM-DD[T]HH:mm:ss'));

Upvotes: 1

top.dev
top.dev

Reputation: 457

try this

var dbDate = '2016-01-30 14:36:00';
var format = 'YYYY-MM-DDTHH:mm:ss';
var date = moment(dbDate, format);
var isValid = date.isValid() // true
console.log(isValid, date.year(), date.month(), date.date())

Upvotes: 1

Related Questions