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));
});
Pradip R.
  • 2,297
  • 2
  • 16
  • 39
Dazza
  • 767
  • 1
  • 11
  • 27

3 Answers3

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.

Mike
  • 12,186
  • 8
  • 41
  • 64
  • 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));
});
Gautam Sheth
  • 30,881
  • 1
  • 35
  • 62
0

Load moment.js and do this:

var date = item.DeviceAvailableFrom;
var LifeCycleStart = moment(date).format('DD-MM-YYYY');
harshal gite
  • 1,474
  • 1
  • 12
  • 24
  • 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? – samolpp2 May 17 '19 at 06: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 '19 at 13:33