1

In a for loop, I want to get different dates for every item. I had implemented the following and it was working fine:

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

I was getting:

2017-09-01T20:33:35.245Z
2017-08-01T19:07:15.510Z
2017-07-28T19:43:11.590Z

As I want to convert to date format and get only month, date, year and time, I implement this:

//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);

But it keeps bringing the same date for every element:

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)

I don't what I am missing! Thanks a lot.

yuriy636
  • 11,171
  • 5
  • 37
  • 42
  • The value returned by [*Date.prototype.toString*](http://ecma-international.org/ecma-262/8.0/#sec-date.prototype.tolocaletimestring) is implementation dependent, so there is no guarantee that using it with a fixed *substr* component will provide a consistent result. See [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Sep 04 '17 at 00:35

2 Answers2

0

Okay, use the following code instead:

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);

So, the issue is you are not using Date() with the new keyword.

Kshitij Mittal
  • 2,698
  • 3
  • 25
  • 40
  • This should be posted as a comment as your post isn't really a solution. It's a recommendation to use a plugin with no detail of how this will be useful or how to impliment your recommendation to achieve the OPs expected outcome. – NewToJS Sep 02 '17 at 21:32
  • Sure, I was actually working on the code. I will edit the answer to add the jsfiddle in just a while or remove the answer if it doesn't work out. – Kshitij Mittal Sep 02 '17 at 21:34
  • Then you should plan your answer before posting otherwise you will have comments like this. Not my fault you decided to post a recommendation/incomplete answer. – NewToJS Sep 02 '17 at 21:36
  • @Gabriela, I have edited the answer. the new code should give you the right answers. Please check. – Kshitij Mittal Sep 02 '17 at 22:05
  • It works!! It was not only "new" but also converting toString. Thanks a lot! – Gabriela Moura Sep 03 '17 at 04:25
  • The value returned by [*Date.prototype.toString*](http://ecma-international.org/ecma-262/8.0/#sec-date.prototype.tolocaletimestring) is implementation dependent, so there is no guarantee that this will give a consistent result. See [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Sep 04 '17 at 00:34
0

Use the new keyword before Date(), without it, you're just creating today's date, then convert them to strings in order to use substr with them :

var result = {};

var date = new Date("2017-09-01T20:33:35.245Z").toString();
result.date = date.substr(4, 17);
result.shortDate = result.date.substr(4, 11);

var expiration = new Date("2017-08-01T19:07:15.510Z").toString();
result.expiration = expiration.substr(4, 11);

var updated = new Date("2017-07-28T19:43:11.590Z").toString();
result.updated = updated.substr(4, 11);

console.log(result);
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55