3

I'm querying a SharePoint list using a JavaScript file and returning the results to a HTML page. All the results are coming back fine, but the results from the date column 'DeviceAvailableFrom' is coming back as NaN (aka Not a Number).

I'm using the Moment.js library but I'm new to it so don't know wuite how to format the var line. I want the date to come out DD-MM-YY. The current code which is returning the NaN is:

 var LifeCycleStart = new Date(item.DeviceAvailableFrom); 

How would I work the Moment formatting in to get the desired results?

Thanks for any help.

5
  • If DeviceAvailableFrom has the right values but in the wrong format, you can wrap it with moment by passing var wrapper = moment(LifeCycleStart); on the next line.
    – moe
    Commented Jun 6, 2017 at 9:48
  • So it would be: var LifeCycleStart = new Date(item.DeviceAvailableFrom); var wrapper = moment(LifeCycleStart); where would the formatting go?
    – Dazza
    Commented Jun 6, 2017 at 10:00
  • Added the complete js below as an answer.
    – moe
    Commented Jun 6, 2017 at 10:12
  • First check your data and list all those dates in F12 with console.log( typeof item.DeviceAvailableFrom , item.DeviceAvailableFrom ) to check what you get from SP Commented Jun 6, 2017 at 10:40
  • Oddly enough when I put the console.log line in it knocks out the rest of the results
    – Dazza
    Commented Jun 6, 2017 at 11:31

2 Answers 2

2

If DeviceAvailableFrom has the right values but in the wrong format, you can wrap & format it with moment on the next line.

var LifeCycleStart = new Date(item.DeviceAvailableFrom);
var wrapper = moment(wrapper).format("DD-MM-YY");

I haven't tested the script.

Edit: Ty Danny '365CSI' Engelman for pointing out the double-conversion.

3
  • You are then doing the Moment conversion twice.. Commented Jun 6, 2017 at 10:43
  • Thanks. If I add that code it knows the rest of he results out. However, if I add: var LifeCycleStart=new Date(); LifeCycleStart.toString('dddd,MMM,yyyy'); It brings up todays date and time, so I need to find a way to parse in the results from the DeviceAvailableFrom column...
    – Dazza
    Commented Jun 6, 2017 at 10:57
  • 'var LifeCycleStart = new Date(item.DeviceAvailableFrom); var wrapper = moment(wrapper).format("DD-MM-YY");' Thanks. That code knocks out he rest of the results.
    – Dazza
    Commented Jun 6, 2017 at 11:27
1

If the date is blank it is not a Number

var LifeCycleStart = item.DeviceAvailableFrom;
if (LifeCycleStart) LifeCycleStart=new Date(item.DeviceAvailableFrom); 

And you don't need MomentJS

SharePoint adds a String.format function
so you can use:

String.format('{0:dd}-{0:MM}-{0:yyyy}',new Date('2015-10-07T20:39:54Z'));

Note: String.format is defined in msajaxbundle.js, loaded even before most of the JavaScript files so safe to use without SOD requirements or anything.
It was modelled after the C# and VB implementations, so the MSDN documentation applies
(for the major part; it does not do the alignment stuff as that makes no sense in HTML)

String.format("{0:i}",new Date());  outputs: Wed Oct 07 2015 20:39:54 GMT+0200 (W. Europe Daylight Time)
String.format("{0:F}",new Date());  outputs: Wednesday, 07 October 2015 20:39:54
String.format("{0:f}",new Date());  outputs: Wednesday, 07 October 2015 20:39
String.format("{0:D}",new Date());  outputs: Wednesday, 07 October 2015
String.format("{0:s}",new Date());  outputs: 2015-10-07T20:39:54
String.format("{0:d}",new Date());  outputs: 10/07/2015
String.format("{0:dd}",new Date());  outputs: 07
String.format("{0:ddd}",new Date());  outputs: Wed
String.format("{0:dddd}",new Date());  outputs: Wednesday
String.format("{0:m}",new Date());  outputs: October 07
String.format("{0:M}",new Date());  outputs: October 07
String.format("{0:MM}",new Date());  outputs: 10
String.format("{0:MMM}",new Date());  outputs: Oct
String.format("{0:MMMM}",new Date());  outputs: October
String.format("{0:Y}",new Date());  outputs: 2015 October
String.format("{0:y}",new Date());  outputs: 2015 October
String.format("{0:yy}",new Date());  outputs: 15
String.format("{0:yyyy}",new Date());  outputs: 2015
String.format("{0:gg}",new Date());  outputs: A.D.
String.format("{0:T}",new Date());  outputs: 20:39:54
String.format("{0:t}",new Date());  outputs: 20:39
String.format("{0:HH}",new Date());  outputs: 20
String.format("{0:mm}",new Date());  outputs: 39
String.format("{0:ss}",new Date());  outputs: 54

It does more then just Dates:

MSDN String.format() Documentation

J5 iJS string format top20 iDate

1
  • Cheers, I've added: var LifeCycleStart = item.DeviceAvailableFrom; if (LifeCycleStart) LifeCycleStart=new Date(item.DeviceAvailableFrom); String.format('{0:dd}-{0:MM}-{0:yyyy}',new Date('2015-10-07T20:39:54Z')); and I'm getting 'null' when there isn't a date and NaN when there is a date?
    – Dazza
    Commented Jun 6, 2017 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.