Javascript code inside my aspx page layout were smarter than i thought, and worked well on different regional settings and date format out of the box

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/264577

Question

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?

No correct solution

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top