Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

javascript - 无法通过REST API使JS Moment在SharePoint中工作(Can't Get JS Moment to Work in SharePoint with REST API)

I'm working in SharePoint 2013 using REST API to retrieve Date field values.

(我正在使用REST API在SharePoint 2013中工作,以检索“日期”字段值。)

I need to get the from now value of those fields and so I am using JS Moment, but so far I can't the moment code to work at all.

(我需要获取这些字段的从现在开始的值,因此我正在使用JS Moment,但到目前为止,我暂时无法正常工作。)

Here's some snippets of my SharePoint REST API code:

(这是我的SharePoint REST API代码的一些片段:)

url:/sites/regulatory3/testdashboard/_api/web/lists/GetByTitle('Contacts')/items?"$Select=Created,OData__x0031_st_x0020_Draft_x0020_Test",

$.each(data.d.results, function (key, value) { 

var createdDate1 = $.format.date(value.Created, 'dd/MM/yyyy');
var createdDate2 = createdDate1.toString();
var createdDate3 = new Date(createdDate2);

var firstDueDate1 = $.format.date(value.OData__x0031_st_x0020_Draft_x0020_Test, 'd MMM yyyy');
var firstDueDate2 = firstDueDate1.toString();
var firstDueDate3 = new Date(firstDueDate2);

Here's what I have tried:

(这是我尝试过的:)

var createdDate4 = moment(createdDate3).toNow();
var firstDueDate4 = moment(firstDueDate3).toNow();

test1 = moment([2019, 1, 29]).fromNow(); //example from tutorials - Not relevant to what I'm doing
test2 = moment().subtract(5, 'h'); //example from tutorials - Not relevant to what I'm doing

Finally, here's my include for Moment:

(最后,这是我对Moment的包含:)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-dateFormat/1.0/jquery.dateFormat.min.js" type="text/javascript"></script>

Has anyone else seen this problem?

(还有其他人看到过这个问题吗?)

If so please provide guidance and code examples.

(如果是这样,请提供指导和代码示例。)

  ask by Tom Molskow translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want a from now value for ItemCreated Date which compare with Today, just use moment(item.Created).fromNow() will be working, please refer the following code:

(如果您想要与今天比较的ItemCreated日期的从现在开始的值,只需使用moment(item.Created).fromNow()即可,请参考以下代码:)

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
<script type="text/javascript">
$.ajax  
    ({  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('MyList')/items",  
        type: "GET",  
        headers:  
        {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val() 
        },  
        cache: false,  
        success: function(data)   
        {    
            for (var i = 0; i < data.d.results.length; i++)   
            {  
                var item = data.d.results[i];
                var createdDate = moment(item.Created);
                console.log(createdDate.fromNow());
            }  
        },  
        error: function(data)  
        {  
            console.log(data.responseJSON.error);  
        }  
    });  
</script>

在此处输入图片说明

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...