0

I am working on an enterprise wiki site collection inside SP on-premises 2013. and inside the page layout i added this javascript, which get 2 date fields values from the current page (expirydate & modifieddate) + the current date/time:-

function showexpiry(){

    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var list = web.get_lists().getById(_spPageContextInfo.pageListId);
    var item = list.getItemById(_spPageContextInfo.pageItemId);

    ctx.load(list);
    ctx.load(item);

    ctx.executeQueryAsync( 
          function(){ 

            var expirydate = item.get_item('ExpireDate');
            var expiredperiod = new Date(expirydate);
            expiredperiod.setDate(expiredperiod.getDate()+1);           
            var modifieddate = item.get_item('Modified');            
            var currentdate = new Date();
            //code goes here...
            if (expirydate != null && modifieddate  < expirydate && currentdate > expiredperiod )

now i was afraid that since i am using JavaScript to do date comparison , to have different results on different PCs which use different regional settings and date/time format such as dd/mm/yyyy or mm/dd/yyyy.

Now the site collection has this regional settings:- enter image description here

which are compatible with the regional setting for my Pc and date/time format:- enter image description here

so i got the dates from my code in this compatible format expirydate = Tue Jun 11 2019 00:00:00 GMT+0100 (British Summer Time) & currentdate = Thu Jun 13 2019 00:54:15 GMT+0100 (British Summer Time).

my concerns were if i access the site on a PC which uses the US date format, to have my dates in an incompatible formats. but i have noted that even the date values for the site columns and the current date will be compatible, so when i changed the date/format for the PC to use mm/dd/yyyy instead of dd/mm/yyyy (and this format is different from the regional settings inside sharepoint site collection), all the returned dates got the new format (expirtdate + modifieddate + currentdate). so i got the values as follow
expirydate = Mon Jun 10 2019 16:00:00 GMT-0700 (Pacific Daylight Time) & currentdate = Wed Jun 12 2019 16:55:26 GMT-0700 (Pacific Daylight Time). for example the modified date shown inside the page will be 10/06/2019 16:56while the JavaScript will read it as shown in the popup:- enter image description here

so everything went well. so can anyone advice how the JavaScript code, converted the expirtydate + the modifieddateto match the users' PC settings and the currentdate automatically?

0

Hello to work with date in SharePoint with javascript the save option is to get SharePoint site's regional time zone and convert the new Date() function according to the regional zone setting. To get Site Regional setting you refer below code.

var context = SP.ClientContext.get_current();
var web = context.get_web();

var regionalSettings = web.get_regionalSettings();
   context.load(regionalSettings);
   context.executeQueryAsync(
       function () {
           var timeZone = regionalSettings.get_timeZone();
           context.load(timeZone);
           context.executeQueryAsync(
               function () {
                   var info = timeZone.get_information();
                   var offset = (info.get_bias() + info.get_daylightBias()) / 60.0;
                   var serverDateTimeNow =
   new Date(new Date().getTime() - offset * 3600 * 1000).toISOString();
                   console.log("serverDateTimeNow: " + serverDateTimeNow);
               },
               function (sender, args) {
                   console.log(args.get_message());
               }
           );
       },
       function (sender, args) {
           console.log(args.get_message());
       }
   );

And for reference you can refer site

  • but seems in my case the javascript worked correctly without the need to do this extra work.. is my point valid? – SharePoint TestDev Jun 13 at 9:48
  • SharePoint date and javascript date both are correct at their respective end you just to compare them with same time zone to work – Sunil Sahu Jun 13 at 12:53
  • but my question was that my code for getting the current date and for getting the wiki page date columns worked out of the box on all the time zones,,,as described in my question. i mean no need to get the regional settings for the site collection... – SharePoint TestDev Jun 13 at 13:41
  • now i tried your code and the current date will always be in this format serverDateTimeNow: 2019-06-13T18:45:13.756Z regardless of the user regional settings, and this format will not be compatible with the date format used inside the expirydate and modifieddate.. as in my code the 3 dates will always be compatible regardless of the users' PCs settings – SharePoint TestDev Jun 13 at 17:54

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.