Javascript:将datetime转换为DD/MM/YYYY-时间? [英] Javascript: convert datetime to DD/MM/YYYY - Time?

查看:89
本文介绍了Javascript:将datetime转换为DD/MM/YYYY-时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的datetime:

I have a datetime that looks like this:

2017-04-17 18:26:03

如何使用javascript或jquery将其转换为这种格式:

How can I convert this to this format using javascript or jquery:

17/04/2017 18:26

我发现了一个我认为可能会帮助我的问题,但是答案正在转换时间戳,但我的不是时间戳.

I found this question which I thought might help me but the answers are converting a timestamp but mine is not a time stamp.

如何转换DateTime值在jQuery中为dd/mm/yyyy?

推荐答案

您可以使用简单的字符串和数组操作.

You can use simple string and array manipulation.

const dateTime = '2017-04-17 18:26:03';
const parts = dateTime.split(/[- :]/);
const wanted = `${parts[2]}/${parts[1]}/${parts[0]} ${parts[3]}:${parts[4]}`;
console.log(wanted);

其他:如果您没有支持模板的环境文字,那么您可以这样编写.

Additional: If you don't have an environment that supports Template Literals then you can write it like this.

const dateTime = '2017-04-17 18:26:03';
const parts = dateTime.split(/[- :]/);
const wanted = parts[2] + '/' + parts[1] + '/' + parts[0] + ' ' + parts[3] + ':' + parts[4];
console.log(wanted);

这篇关于Javascript:将datetime转换为DD/MM/YYYY-时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆