3

Scratching my head on this.

I've got a JavaScript file querying a SharePoint list. It returns the results fine to a HTML page, except the Date field which comes back in the format /Date(1500595200000)/ which is the ISO-8601 format. However when I try format it using Moment.JS it knocks out the rest of the results brought in from the getDeviceDeatils js file. If I changed the column type in SharePoint from Date to Single Line of Text the date shows up fine.

I've tried formatting it thus:

var LifeCycleStart = moment(item.DeviceAvailableFrom).format('DD-MM-YY');

but that knocks out the results.

Any ideas? JS below and screenshot of the results included.

function getDeviceDetails() {
var txtTitle = "";
var txtOverview = "";
var txtAccessories = "";
var txtDevicetype = "";
var txtTypicalDeviceUsage ="";
var txtKnownSystemIssues ="";
var txtLifeCycles = "";
var tempLCS2 = "";

var query = "http://collaboration-dev.norgine.com/sites/it/SystemInventory/_vti_bin/listdata.svc/Devices?$expand=LifeCycleStatus&$filter=Id eq " + window.DeviceId + "";
var call = $.ajax({
        url: query,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }       
    });
call.done(function (data,textStatus, jqXHR){
$.each(data.d.results, function(index, item) {
        var tempID = item.Id;
        var tempTitle = item.Title;
         var LifeCycleStart = item.DeviceAvailableFrom;
        // var LifeCycleStart = moment(item.DeviceAvailableFrom).format('DD-    MM-YY');
        var LifeCycleStatus = item.LifeCycleStatusValue;
        var DeviceOverView = item.Description;
        var AccessDetails = item.Accessories;
        var DeviceKind = item.Devicetype;
        var Usage = item.TypicalUsage;
                    txtTitle = "<p>" + LifeCycleStart + "</p><p>" + LifeCycleStatus + "</p>";
        txtOverview = "<p>" + DeviceOverView + "</p>";
        txtAccessories = "<p>" + AccessDetails + "</p>";  
        txtDevicetype = "<p>" + DeviceKind  + "</p>";
        txtTypicalDeviceUsage = "<p>" + Usage + "</p>";
       // txtKnownSystemIssues = "<p>" + KnownSystem + "</p>"
    });
    $('#devicedetails').append($(txtTitle));  
    $('#deviceoverview').append($(txtOverview));
    $('#devicekind').append(txtDevicetype);
    $('#deviceacc').append(txtAccessories);
    $('#deviceuse').append(txtTypicalDeviceUsage);
});
call.fail(function (jqXHR,textStatus,errorThrown){
    alert("Error retrieving data: " + jqXHR.responseText);
});

JavaScript results}

2
  • If you're already using moment, try this: var LifeCycleStart = moment(new Date(item.DeviceAvailableFrom)).format('DD-MM-YY'); Commented Jun 8, 2017 at 14:42
  • any reason you're still using _vti_bin and not _api? Commented Jun 8, 2017 at 14:43

2 Answers 2

1

Moment wants a Number, not a String:

So use:

moment(Number(item.DeviceAvailableFrom)).format('DD-MM-YY');
7
  • Cheers Danny. Just added that and it knocked the results out. It seems that everything I do around the DeviceAvailableFrom variable makes the rest of the results disappear? Commented Jun 8, 2017 at 9:13
  • Checked on debugger and it says: Uncaught ReferenceError: moment is not defined but in the html file it's linked in the header: <script src="/it/SystemInventory/SiteAssets/scripts/moment.min.js"></script> Commented Jun 8, 2017 at 9:19
  • Then your issue has nothing to do with dates or your code; you have not loaded MomentJS. In your other question I already showed you how to use SPs own date formatting Commented Jun 8, 2017 at 9:30
  • Okay, understood and excuse my ignorance but what would I need to do to load Moment ? Commented Jun 8, 2017 at 9:31
  • Typ load library in the StackOverflow search box.. it is just like loading jQuery Commented Jun 8, 2017 at 9:36
0

Another way to display SharePoint date field is that instead of using moment.js, you can use date.js (open source JS library) and this works smoothly for me. So you should write like:

var LifeCycleStart = Date.parse(item.DeviceAvailableFrom).toString('MM/dd/yyyy')

Download DateJS from Here

2
  • MomentJS is also open source, DateJS only introduces another syntax, it is still a library to be loaded Commented Jun 8, 2017 at 10:40
  • Yes, right Danny. Since date.js worked superfine for me so thought of suggesting this as alternative Commented Jun 8, 2017 at 11:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.