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?
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?
Share Improve this question asked Oct 6, 2017 at 15:46 user8398743user8398743 1- You can simply concat your strings and then parse it with moment. – Cupkek05 Commented Oct 6, 2017 at 15:52
2 Answers
Reset to default 3your 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']
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]);