【问题标题】:Need to Format Date in a SharePoint Content Search Web Part Display Template需要在 SharePoint 内容搜索 Web 部件显示模板中设置日期格式
【发布时间】:2019-08-14 01:10:57
【问题描述】:

我正在使用 SharePoint 2013,并且为内容搜索 Web 部件创建了一个自定义显示模板。我的三个字段使用日期,并且所有三个都以长格式返回日期。我想以短格式返回日期,但似乎无法正常工作。

我已经尝试了这些博客文章中的建议:

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2014/02/26/useful-javascript-for-working-with-sharepoint-display-templates-spc3000-spc14.aspx

https://sharedpointtips.blogspot.com/2015/01/sharepoint-2013-display-template.html

https://sharepoint.protiviti.com/blog/Lists/Posts/Post.aspx?ID=132

http://sharepointfordeveloper.blogspot.com/2015/11/shareppoint-2013-ctx-variable-and-its_17.html

在标题中:

'Review Date'{Review Date Label}:'ReviewDateOWSDATE',

在 JavaScript 部分我试过这个:

var shortDate = Srch.U.toFormattedDate(dateresponsedue,'ShortDatePattern');

还有这个:

var shortDate2 = Srch.U.toFormattedDate(shortDate, 'ShortDatePattern');```

And this:

```var shortDate = Srch.U.toFormattedDate(ctx.CurrentItem.DateResponseDueforReviewOWSDATE, 'ShortDatePattern');```

This is my  display code:

```<td rowspan="3" width="85px" style="text-align:center;"> _#= shortDate =#_ </td>'''



I need the date display to change from something like this:

2019-07-11T05:00:00Z;7/11/2019 5:00:00 AM 

To something like this:

2019-07-11

【问题讨论】:

    标签: javascript sharepoint-2013 display-templates


    【解决方案1】:

    考虑到 Sharepoint 基本日期格式将始终为 YYYY-MM-DDTHH:MM:SS ,如果您只想获取相同格式的日期,您可以这样做:

    var fullDate = ctx.CurrentItem.DateResponseDueforReviewOWSDATE;
    var shortDate = fullDate.split('T')[0];
    
    /*
    in this case 
    2019-07-11T05:00:00Z;7/11/2019 5:00:00 AM  returns  2019-07-11
    */
    

    如果您想将日期重新转换为不同的格式,您可以将上述格式转换为常规的 javascript Date 对象并使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-09
      • 2017-11-29
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多