[英]How do I format a date string without changing the culture?
I am getting string data from a Google Calendar feed. 我正在从Google日历供稿获取字符串数据。 The date is already set, by parameter, with the desired timezone.
日期已通过参数设置为所需的时区。
2014-05-24T07:00:00.000-04:00
I know there are wonderful libraries like moment.js and date.js this will help format my date, but they also work with a Date object, which throws my date into a client's culture. 我知道有很多很棒的库,例如moment.js和date.js,它们可以帮助格式化我的日期,但是它们也可以与Date对象一起使用,这使我的日期融入了客户的文化。 At that point I am then juggling offsets.
那时,我正在努力抵消。 Would rather avoid that.
宁愿避免这种情况。
Other than a lot of conditional string manipulation, is there a simple way to do this, or I am oversimplifying (again)? 除了大量的条件字符串操作外,还有没有一种简单的方法可以执行此操作,或者我过于简化了(再次)?
Example: 例:
2014-05-24T07:00:00.000-04:00 to May, 24, 2014 - 7:00 AM 2014-05-24T07:00:00.000-04:00至2014年5月24日 -7:00 AM
The following short code will parse your date, using the values present, without offsetting by the timezone: 以下简短代码将使用存在的值来解析您的日期,而不用时区偏移:
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var res = /(\d+)\-(\d+)\-(\d+)T(\d+)\:(\d+)/
.exec('2014-05-24T07:00:00.000-04:00');
var am = (res[4] < 12);
var date = months[res[2]-1] + ', ' + res[3] + ', ' + res[1];
var time = am ? (parseInt(res[4]) + ':' + res[5] + 'AM') :
(res[4] - 12 + ':' + res[5] + 'PM');
var formatted = date + ' - ' + time;
console.log(formatted);
You can convert this string into a Date object like below, 您可以将该字符串转换为Date对象,如下所示,
new Date("2014-05-24T07:00:00.000-04:00")
Then you can easily convert this date object into your desired format by using any of the jQuery libraries such as jquery.globalize.js ... 然后,您可以使用任何jQuery库(例如jquery.globalize.js)轻松地将此日期对象转换为所需格式。
Here you go: 干得好:
var d = new Date('2014-05-24T07:00:00.000-04:00');
var calendar = {
months: {
full: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
days: {
full: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
short: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
}
};
console.log(calendar.months.short[d.getMonth()]+', '+d.getDate()+', '+d.getFullYear());
A simple function to reformat the string is: 重新格式化字符串的简单函数是:
function reformatDateString(s) {
s = s.match(/\d+/g);
var months = ['January','February','March','April','May','June','July',
'August','September','October','November','December'];
var ampm = s[3]<12? 'AM':'PM';
return months[--s[1]] + ', ' + +s[2] + ', ' + s[0] + ' - ' +
(s[3]%12 || 12) + ':' + s[4] + ' ' + ampm;
}
console.log(reformatDateString('2014-05-24T07:00:00.000-04:00')); // May, 24, 2014 - 7:00 AM
console.log(reformatDateString('2014-05-24T17:00:00.000-04:00')); // May, 24, 2014 - 5:00 PM
console.log(reformatDateString('2014-05-04T00:20:00.000-04:00')); // May, 4, 2014 - 12:20 AM
Which also assumes that you don't want leading zeros on single digit numbers except for the minutes, as a time like 12:5 PM
isn't as readable (to me) as 12:05 PM
. 这还假设您不希望分钟以外的任何一位数字前导零,因为
12:5 PM
这样的时间对我而言不如12:05 PM
可读。
Also you may need to modify the months array, it's not clear in the OP whether you want full month names or abbreviations (Jan, Feb, etc.). 另外,您可能需要修改months数组,在OP中尚不清楚您是否需要完整的月份名称或缩写(Jan,Feb等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.