最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

momentjs - javascript moment separate date and time - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

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']

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]);
发布评论

评论列表(0)

  1. 暂无评论