0

Afternoon

I'm new to using Moment and need to format a date from SharePoint in JavaScript. The variable is LifeCycleStart and I don't know the right way to format it and drop in Moment. Here's what I've got:

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 LifeCycleStatus = item.LifeCycleStatusValue
        var DeviceOverView = item.Description
        txtTitle = "<p>" + LifeCycleStart + "</p><p>" + LifeCycleStatus + "</p>";
        txtOverview = "<p>" + DeviceOverView + "</p>";
    });
    $('#devicedetails').append($(txtTitle));  
    $('#deviceoverview').append($(txtOverview));
});
1

If you have moment loaded just use moment like this:

var LifeCycleStart = moment(item.DeviceAvailableFrom, "MM-DD-YYYY");

You can look at more formats here in the documentation. There are plenty of formats, and you probably will find what you are looking to output.

  • Thanks. So do I replace the current var LifeCycleStart line with the moment version? When I do that, or add the moment line as an extra the information disappears from the table? – Dazza May 16 '17 at 14:36
  • replace what you have: var LifeCycleStart = item.DeviceAvailableFrom , with what I have in my answer. – Mike May 16 '17 at 17:05
1

As you mentioned in comments that you want to format date to DD-MM-YYYY, you dont require moment js to be loaded. SharePoint itself provides the necessary functions to format date as per your requirement.

Danny Engelman, one of the community's top contributors has provided an excellent answer at the below link:

Changing date format using javascript

So, for your case, you can use it as below:

var LifeCycleStart = String.format('{0:dd}-{0:MM}-{0:yyyy}',new Date(item.DeviceAvailableFrom))

So your overall code would be as below:

call.done(function (data,textStatus, jqXHR){
    $.each(data.d.results, function(index, item) {
        var tempID = item.Id;
        var tempTitle = item.Title;
        var LifeCycleStart = String.format('{0:dd}-{0:MM}-{0:yyyy}',new Date(item.DeviceAvailableFrom));
        var LifeCycleStatus = item.LifeCycleStatusValue;
        var DeviceOverView = item.Description;
        txtTitle = "<p>" + LifeCycleStart + "</p><p>" + LifeCycleStatus + "</p>";
        txtOverview = "<p>" + DeviceOverView + "</p>";
    });
    $('#devicedetails').append($(txtTitle));  
    $('#deviceoverview').append($(txtOverview));
});

Based on your comments, can you try below code. I am using the replace method to change the string format:

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.replace(/\//g, '-');
        var LifeCycleStatus = item.LifeCycleStatusValue;
        var DeviceOverView = item.Description;
        txtTitle = "<p>" + LifeCycleStart + "</p><p>" + LifeCycleStatus + "</p>";
        txtOverview = "<p>" + DeviceOverView + "</p>";
    });
    $('#devicedetails').append($(txtTitle));  
    $('#deviceoverview').append($(txtOverview));
});
  • Cheers, now the time displays as "NaN-NaN-NaN" – Dazza May 18 '17 at 10:18
  • can you tell me what the value is in item.DeviceAvailableFrom ? – Gautam Sheth May 18 '17 at 11:10
  • Yes, it's: 11/05/2017 – Dazza May 18 '17 at 12:58
  • so you mean that in the sharepoint list item, the value is 11/05/2017 ? am i correct and you want to make it 11-05-2017 ? – Gautam Sheth May 18 '17 at 13:04
  • Yes, and just want it to display that, all that's coming up at the moment is NaN-NaN-NaN – Dazza May 18 '17 at 13:19
0

Load moment.js and do this:

var date = item.DeviceAvailableFrom;
var LifeCycleStart = moment(date).format('DD-MM-YYYY');
  • how to compare this date with todays date? if todays date is greater than datetimefield's value, i wanna delete that item. how to do this? – SaMolPP May 17 at 6:40
  • LifeCycleStart has the date value from a datetime field. So if you want to compare today's date with this date (LifeCycleStart), you can format today's date in the same format as "LifeCycleStart" and then simply compare. var LifeCycleStart = moment(date).format('DD-MM-YYYY'); var today = moment().format('DD-MM-YYYY'); if(today > LifeCycleStart ){ //item deletion code goes here.. } – harshal gite May 17 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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