简体   繁体   English

使用javascript格式化DATE(可能正则表达式)

[英]Format a DATE with javascript (maybe regex)

I'm using JavaScript and JQuery and I've got a string like this (unchangeable xml response): 我正在使用JavaScript和JQuery,我有一个这样的字符串(不可更改的xml响应):

var str = "2017-01-08T16:06:52+00:00";

How can I convert to a Date like this: 如何转换为这样的日期:

08 january 2017, 16:06:52

Or at least: 或至少:

08 01 2017, 16:06:52

I was trying to use a .replace() like: 我试图使用.replace()如:

str = str.replace(/(\d{4})-(\d{2})-(\d{2})T(\d{8}).*/,'$2 $3 $1, $4');

But it doesn't works. 但它不起作用。 :( :(

To do this you can create a Date() object from the string, then concatenate together a string from the methods the Date() object exposes. 为此,您可以从字符串创建Date()对象,然后将Date()对象公开的方法中的字符串连接在一起。 Try this: 试试这个:

 var str = "2017-01-08T16:06:52+00:00"; var date = new Date(str); var months = [ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" ]; var dateString = ("00" + date.getDate()).slice(-2) + ' ' + months[date.getMonth()] + ' ' + date.getFullYear() + ', ' + ("00" + date.getHours()).slice(-2) + ':' + ("00" + date.getMinutes()).slice(-2) + ':' + ("00" + date.getSeconds()).slice(-2); console.log(dateString); 

Note that this can be made simpler by using a date formatting library (such as MomentJS or DateJS) however including an entire library to format a single date is rather wasteful. 请注意,通过使用日期格式库(例如MomentJS或DateJS)可以使这更简单,但是包括整个库来格式化单个日期是相当浪费的。 If you need to do this repeatedly throughout your site then they may be worth it. 如果您需要在整个网站中反复执行此操作,那么它们可能是值得的。

Also note that in English speaking countries the month names should be capitalised, so consider amending the format for that case. 另请注意,在英语国家/地区,月份名称应大写,因此请考虑修改该案​​例的格式。

Alternatively, if adding a library is an option you could use moment.js . 或者,如果添加库是一个选项,您可以使用moment.js You can call .format and pass how you would like the output to be, for example: 您可以调用.format并传递输出的结果,例如:

 var str = "2017-01-08T16:06:52+00:00"; var momentDate = moment(str); console.log(momentDate.format('DD MMMM YYYY HH:mm:ss')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script> 

Your regular expression isn't quite right, \\d{8} will match 8 consecutive digits but there are colons in there as well, you need something like: 你的正则表达式不太正确, \\d{8}将匹配8个连续数字但是那里也有冒号,你需要类似的东西:

 var str = "2017-01-08T16:06:52+00:00" str = str.replace(/(\\d{4})-(\\d{2})-(\\d{2})T(.{8}).*/, '$2 $3 $1, $4'); console.log(str) 

To replace the month number with the name is just a little more work: 用名称替换月份数字只需要更多工作:

 function reformatDate(s) { var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var b = s.split(/\\D/); return b[2] + ' ' + months[b[1]-1] + ', ' + b[0] + ' ' + b[3] + ':' + b[4] + ':' + b[5]; } var s = "2017-01-08T16:06:52+00:00" console.log(reformatDate(s)); 

Be careful using the Date constructor (and Date.parse) for parsing strings, it's largely implementation dependent. 小心使用Date构造函数(和Date.parse)来解析字符串,它在很大程度上依赖于实现。

If you want to present the date and time in the timezone of the client, then parse the string to a Date, then return it as a string. 如果要在客户端的时区中显示日期和时间,则将字符串解析为Date,然后将其作为字符串返回。 A library like fecha.js is good for that. fecha.js这样的图书馆对此有好处。 It's small and just does parsing and formatting, eg 它很小,只是解析和格式化,例如

var s = '2017-01-08T16:06:52+00:00'
var d = fecha.parse(s,'YYYY-MM-DDTHH:mm:ssZZ');

console.log(fecha.format(d,'dd MMMM, YYYY HH:mm:ss'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM