Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换数据javascript_Javascript_Date - Fatal编程技术网

转换数据javascript

转换数据javascript,javascript,date,Javascript,Date,在for循环中,我希望每个项目都有不同的日期。我实施了以下措施,效果良好: //This retrieves the right dates: result.date = offer.get("createdAt"); result.updated = exchange.get("updatedAt"); result.expiration = exchange.get("expirationDate"); 我得到了: 2017-09-01T20:33:35.245Z 2017-08-01T1

在for循环中,我希望每个项目都有不同的日期。我实施了以下措施,效果良好:

//This retrieves the right dates:
result.date = offer.get("createdAt");
result.updated = exchange.get("updatedAt");
result.expiration = exchange.get("expirationDate");
我得到了:

2017-09-01T20:33:35.245Z
2017-08-01T19:07:15.510Z
2017-07-28T19:43:11.590Z
由于我希望转换为日期格式并仅获取月、日、年和时间,我实现了以下功能:

//This brings the same date every time;
var date = Date(offer.get("createdAt"));
result.date = date.substr(4, 17);
result.shortDate = result.date.substr(4, 11);

var expiration = Date(exchange.get("expirationDate"));
result.expiration = expiration.substr(4, 11);

var updated = Date(exchange.get("updatedAt"));
result.updated = updated.substr(4, 11);
但每个元素的日期都是一样的:

Sep 02 2017 18:08
Sep 02 2017 18:08 (this should be a different date)
Sep 02 2017 18:08 (this should be a different date)

我不知道我错过了什么!非常感谢。

好的,请使用以下代码:

var date = new Date(offer.get("createdAt"));
result.date = date.toString().substr(4, 17);
result.shortDate = result.date.substr(4, 11);

var expiration = new Date(exchange.get("expirationDate"));
result.expiration = expiration.toString().substr(4, 11);

var updated = new Date(exchange.get("updatedAt"));
result.updated = updated.toString().substr(4, 11);

因此,问题是您没有将Date()与new关键字一起使用

好的,请使用以下代码:

var date = new Date(offer.get("createdAt"));
result.date = date.toString().substr(4, 17);
result.shortDate = result.date.substr(4, 11);

var expiration = new Date(exchange.get("expirationDate"));
result.expiration = expiration.toString().substr(4, 11);

var updated = new Date(exchange.get("updatedAt"));
result.updated = updated.toString().substr(4, 11);

因此,问题是您没有将Date()与new关键字一起使用

Date()
之前使用
new
关键字,没有它,您只需创建今天的日期,然后将其转换为字符串,以便使用
substr

var result={};
var日期=新日期(“2017-09-01T20:33:35.245Z”)。toString();
result.date=date.substr(4,17);
result.shortDate=result.date.substr(4,11);
var到期=新日期(“2017-08-01T19:07:15.510Z”)。toString();
result.expiration=expiration.substr(4,11);
var更新=新日期(“2017-07-28T19:43:11.590Z”)。toString();
result.updated=updated.substr(4,11);

控制台日志(结果)
Date()
之前使用
new
关键字,没有它,您只需创建今天的日期,然后将其转换为字符串,以便对其使用
substr

var result={};
var日期=新日期(“2017-09-01T20:33:35.245Z”)。toString();
result.date=date.substr(4,17);
result.shortDate=result.date.substr(4,11);
var到期=新日期(“2017-08-01T19:07:15.510Z”)。toString();
result.expiration=expiration.substr(4,11);
var更新=新日期(“2017-07-28T19:43:11.590Z”)。toString();
result.updated=updated.substr(4,11);

控制台日志(结果)这应该作为评论发布,因为您的帖子并不是真正的解决方案。这是一个使用插件的建议,没有详细说明这将如何有用,或者如何实现您的建议,以实现OPs的预期结果。当然,我实际上是在编写代码。我会在一段时间内编辑答案以添加JSFIDLE,如果答案不起作用,我会删除答案。然后你应该在发布之前计划好答案,否则你会有这样的评论。不是我的错,你决定发布推荐/不完整的答案。@Gabriela,我已经编辑了答案。新代码应该会给你正确的答案。请检查。它能用!!它不仅是“新的”,而且还转换成字符串。谢谢!这应该作为评论发布,因为你的帖子并不是真正的解决方案。这是一个使用插件的建议,没有详细说明这将如何有用,或者如何实现您的建议,以实现OPs的预期结果。当然,我实际上是在编写代码。我会在一段时间内编辑答案以添加JSFIDLE,如果答案不起作用,我会删除答案。然后你应该在发布之前计划好答案,否则你会有这样的评论。不是我的错,你决定发布推荐/不完整的答案。@Gabriela,我已经编辑了答案。新代码应该会给你正确的答案。请检查。它能用!!它不仅是“新的”,而且还转换成字符串。谢谢!返回的值取决于实现,因此不能保证将其与固定的substr组件一起使用将提供一致的结果。请参阅,返回的值取决于实现,因此不能保证将其与固定的substr组件一起使用将提供一致的结果。看见