0

I've found partial solutions but retrieving the date from a SharePoint list adds one more level of fun. The code I've got which is returning NaN is:

var LifeCycleStart = new Date(item.DeviceAvailableFrom); 

Obviously the SharePoint column is DeviceAvailableFrom.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Dazza
  • 139
  • 11
  • Can you include an example of `DeviceAvailableFrom`'s value(s) prior to `new Date()`? It's probably not in a format that the browser recognizes outright. – Jonathan Lonowski Jun 05 '17 at 14:59
  • No problem, it's: 11/05/2017 – Dazza Jun 05 '17 at 15:05
  • Do you get the expected date when trying to value directly – `new Date("11/05/2017")`? – Is that in `M/D/Y` or `D/M/Y`? Are you or your users in an area that typically uses the other of the two formats? – If you're willing to use a library, MomentJS allows for [specifying a format when parsing](https://momentjs.com/docs/#/parsing/string-format/), so you can ensure it matches SharePoint for all users. – Jonathan Lonowski Jun 05 '17 at 15:28
  • @Dazza What type of object is `item`? How did you obtain a reference to it in your JavaScript? – Thriggle Jun 05 '17 at 17:52
  • Please don't give random strings to the built-in parser. Always parse the string yourself and tell the parser the format. A library can help ([*moment.js*](https://momentjs.com/), [*fecha.js*](https://github.com/taylorhakes/fecha)), but if you're only dealing with a single format a simple function can suffice. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results?s=1|7.9474) – RobG Jun 06 '17 at 01:22
  • When I use Moment JS and include the line var LifeCycleStart = moment (item.DeviceAvailableFrom).format('MMMM Do YYYY, h:mm:ss a'); it knocks out the rest of the results. What am I doing wrong? – Dazza Jun 06 '17 at 07:50

1 Answers1

0

I'm not familiar with Sharepoint, but I'd first check that the value of item.DeviceAvailableFrom is either a positive integer, or a string containing a date that Date's constructor is able to parse.

thephpdev
  • 1,097
  • 10
  • 25
  • This is not an answer, it should be a comment. – RobG Jun 06 '17 at 01:26
  • @RobG, I don't see how it isn't an answer. The comments are for asking for more information etc. He asked why `NaN` is returned when he runs that code, I say check the value that is being passed to the constructor, and if that isn't valid, I've answered his question as to why it happens. – thephpdev Jun 08 '17 at 23:16