简体   繁体   English

在JS中将字符串格式化为日期

[英]Formatting String to Date in JS

I'm sure this is a simple question, but I can't for the life of me solve it. 我敢肯定这是一个简单的问题,但是我一生无法解决。

I have a JSON object as so: 我有一个JSON对象:

    {
    "_id": {
        "$oid": "57cb5aac9bd9a31100c793d1"
    },
    "reminders": [
        "2014-03-12T12:00:00",
        "2014-03-12T13:37:27",
        "2014-03-12T13:37:27",
        "2014-03-12T22:14:27"
    ],
    "user": "xxx"
}

I want to parse the date from the reminders is JS to a date object in a loop, as so. 我想从提醒中将日期解析为JS到循环中的日期对象。

for (var i = userSchedule.reminders.length - 1; i >= userSchedule.reminders.length - 1; i++)
    {
    var date = new Date(userSchedule.reminders[i]);
    }

But it just displays invalid date whenever I log it. 但是,只要我登录,它只会显示无效的日期。 Any ideas? 有任何想法吗?

Though its not answer but why have you used user_schedule.reminders and userSchedule.reminders and your for loop will loop only once with correct data since your loop begins with i=3; 尽管它没有答案,但是为什么您使用了user_schedule.remindersuserSchedule.reminders并且for循环仅以正确的数据循环一次,因为您的循环以i = 3开始 which is index for last element of userSchedule.reminders[3] and when you loop next it will go beyond the scope of your array reminders 这是userSchedule.reminders[3]最后一个元素的索引,当您下一次循环时,它将超出数组提醒的范围

for date time manipulation I strongly recommend using http://momentjs.com 对于日期时间操作,我强烈建议使用http://momentjs.com

where you can do 你可以在哪里做

moment("your date string") 

Or 要么

moment("your date string","your date format") 

Something here is not as it seems, because calling the date constructor in both Chrome and Node.JS returns the correct date for me. 事情似乎并非如此,因为在Chrome和Node.JS中都调用日期构造函数会为我返回正确的日期。 I also tried it in the JSBin below. 我也在下面的JSBin中尝试过。

https://jsbin.com/fomagugiwe/edit?html,output https://jsbin.com/fomagugiwe/edit?html,输出

I would log the value going into the date constructor, just to ensure that the value being used is of the correct format. 我会将值记录到日期构造函数中,只是为了确保所使用的值格式正确。 Could you also provide the Node version you are using for this script for further testing.. 您是否也可以提供用于此脚本的Node版本以进行进一步测试。

Your loop is broken, it seems you're trying to iterate from 0 to userSchedule.reminders.length - 1 , so: 您的循环中断了,似乎您正在尝试从0迭代到userSchedule.reminders.length - 1 ,所以:

for (var i=0; i < userSchedule.reminders.length; i++) {
  // do stuff
}

Also, parsing date strings with the Date constructor (and Date.parse, they are equivalent for parsing) is not recommended due to variances in implementations. 另外,由于实现中的差异,也不建议使用Date构造函数(和Date.parse,它们等效于解析)来解析日期字符串。 You should parse the string manually, a library can help but isn't necessary if you have only one format to parse. 您应该手动解析该字符串,如果您只有一种格式可以解析,则库可以提供帮助,但不是必需的。

A date string like "2014-03-12T12:00:00" should be treated as "local" (ie the host time zone offset should be used when calculating the time value), however not all implementations will do that. 像“ 2014-03-12T12:00:00”这样的日期字符串应被视为“本地”(即,在计算时间值时应使用主机时区偏移量),但是并非所有实现都可以这样做。 A small library like fecha.js makes it simple: fecha.js这样的小图书馆很简单:

var d = fecha.parse('2014-03-12T12:00:00','YYYY-MM-DDTHH-mm-ss');

You can also use moment.js , but it's likely overkill for what you need. 您也可以使用moment.js ,但是对于您所需的内容可能会显得有些过时 There are many other parsing and formatting libraries available that are suitable too. 还有许多其他合适的解析和格式化库。

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

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