简体   繁体   English

将字符串转换为日期

[英]Converting a string into a date

I have the following script which joins 2 feeds and displays them on the screen 我有以下脚本,它将2个提要加入并在屏幕上显示它们

$.when( //get feed 1, //get feed 2 ).done(function(a1, a2){

    var data = a1[0]response.Data.feed.entries.concat(a2[0].responseData.feed.entries);

    var sorted = data.sort(function(a, b) {
        if(a.publishedDate > b.publishedDate) {
            return 1
        }
        if(a.publishedDate < b.publishedDate) {
            return -1
        }
        return 0
    });

    for( i = o; i <= sorted.length - 1; i++ ) {
        document.write(sorted[i].title);
        document.write(sorted[i].publishedDate);
    }

});

This returns all the rows, but it doesn't sort the rows. 这将返回所有行,但不会对行进行排序。 The sorting seems completely random. 排序似乎完全是随机的。 I'm assuming it's because the dates are formatted as follows in the JSON data: 我假设这是因为日期在JSON数据中的格式如下:

Mon, 23 Sep 2013 04:37:45 -0700
  1. What does that -0700 mean -0700是什么意思
  2. How do I convert that date string into a proper date object so I can sort the results correctly? 如何将日期字符串转换为正确的日期对象,以便可以正确地对结果进行排序?
  1. -0700 means the UTC offset UTC-07:00 . -0700表示UTC偏移量UTC-07:00
  2. I'm strongly recommend you to use Moment.js library to deal with date and time, formatting and conversions to make your "Date & Time JavaScript Life" easy and funny. 我强烈建议您使用Moment.js库处理日期和时间,格式和转换,以使“ Date&Time JavaScript Life”变得轻松有趣。

As Alnitak said this particular format is accepted by Date.parse therefore if you use one of accepted formats you can just use native JavaScript for sorting. 正如Alnitak所说的那样, Date.parse接受这种特定格式,因此,如果您使用一种接受的格式,则可以使用本机JavaScript进行排序。

var dateStrings,
    sortDates;

dateStrings = [
    'Mon, 23 Sep 2013 04:37:45 -0700',
    'Sun, 22 Sep 2013 05:27:32 +0300',
    'Mon, 23 Sep 2013 03:14:17 -0700'
];

sortDates = function(dateStrings) {
    return dateStrings.sort(function(a, b) {
        return new Date(a) - new Date(b);
    });
};

console.log(sortDates(dateStrings));

Fiddle 小提琴

The variable sorted in your code snippet could be properly retrieved in this way: 可以通过以下方式正确检索代码段中sorted的变量:

var sorted = data.sort(function(a, b) {
    return new Date(a.publishedDate) - new Date(b.publishedDate);
});

You can pass the string to new Date(...) to convert to a real Date object. 您可以将字符串传递给new Date(...)以转换为实际的Date对象。

To sort however you also need to pass a specific sort function because default Javascript sort on arrays just converts elements to string and compares the result of conversion (thus any "Mon" day will happen to be placed before any "Sun" day). 但是,要进行排序,您还需要传递一个特定的排序函数,因为对数组的默认Javascript sort只是将元素转换为字符串并比较转换结果(因此,任何"Mon"天都将放在任何"Sun"星期日"Sun"天之前)。

Something that should work is 应该起作用的是

dates.sort(function(a, b){ return a - b; });

Well, -0700 means.. is 7 hours earlier than Greenwich Mean ...yor can check more in wikipedia 好吧,-0700表示.. 7 hours earlier than Greenwich Mean ...您可以在Wikipedia中查看更多内容

And if you want to convert any date properly, i strongly recommend you to use the library DateJS ( http://www.datejs.com/ ) 而且,如果您想正确地转换任何日期,我强烈建议您使用库DateJS( http://www.datejs.com/

You can use syntatic sugarr..!! 您可以使用合成糖。 to create your object... 创建您的对象...

Date.parse('Thu, 1 July 2013 20:20:20');

voila.. its very easy... 瞧..这很容易...

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

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