user8398743
user8398743

Reputation:

javascript moment separate date and time

I am using moment-timezone and this is the code:

$('#myDiv').text(moment.tz('America/Los_Angeles').format('DD-MM-YYYY HH:mm:ss'));

This will output something like this:

06-10-2017 08:44:15

What I want to do is to put the date and time in 2 different elements but at this point it's all together in the same output.

How can I do this with moment?

Upvotes: 1

Views: 6477

Answers (2)

Sergio Ajanel
Sergio Ajanel

Reputation: 169

your can use

var datetime = moment.tz('America/Los_Angeles').format('DD-MM-YYYY HH:mm:ss').split(' ');

and the value of datetime is ['06-10-2017', '08:44:15']

Upvotes: 3

mrogers
mrogers

Reputation: 1217

You could do it like this:

$('#myDateDiv').text(moment.tz('America/Los_Angeles').format('DD-MM-YYYY'));
$('#myTimeDiv').text(moment.tz('America/Los_Angeles').format('HH:mm:ss'));

Or if you wanted to call moment only once you could split up the result by the space between the date and time:

var datetime = moment.tz('America/Los_Angeles').format('DD-MM-YYYY HH:MM:SS').split(' ');
$('#myDateDiv').text(datetime[0]);
$('#myTimeDiv').text(datetime[1]);

Upvotes: 3

Related Questions