简体   繁体   English

如何在不更改区域性的情况下格式化日期字符串?

[英]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:002014年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.

相关问题 将日期 object 格式化为字符串而不更改小时 - format date object to string without changing the hour 如何确定字符串是否为日期格式? - How do I determine if a string is date format? 如何将JSON字符串日期转换为自定义格式日期? - How do I convert JSON string date to a custom format date? 更改日期字符串的格式 - Changing the format of a date string 如何格式化JavaScript日期与浏览器文化? - How format JavaScript Date with regard to the browser culture? 在JavaScript中,如何将字符串转换为Date? 字符串可能具有不同的文化格式 - In JavaScript, How to convert string into Date ? Where string may have different culture format 如何验证字符串值的格式为“ \\ / Date(1239018869048)\\ /”? - How do I validate that a string value is in the format “\/Date(1239018869048)\/”? 如何将 /Date(1606867200000)/ 这样的日期转换为 dd/MM/yyyy 格式的字符串 - How do I convert a date like this /Date(1606867200000)/ to a string in format dd/MM/yyyy 使用正则表达式,如何将日期时间字符串转换为短日期格式? - Using regular expressions, how do I convert a date time string to a short date format? 我如何使用javascript更改日期的时区而不更改时间 - how do i use javascript change timezone of date without changing the time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM