2

How do I format a date in a customised search Display Template to show the date in my Region rather than UTC.

I use the following XSL in Content Query Web Parts to present the date in my timezone, but what do I use in a customised Display Template to do the same?

ddwrt:FormatDate(@StartDate, 2057, 3)

I have tried the following but this only returns the UTC date.

var StartDate = $getItemValue(ctx, "RefinableDate05");

var StartDate1 = new Date(StartDate.inputValue);

var StartDate2 = StartDate1.format('MM-dd-yyyy hh:mm:ss tt');
3

If you retrieve a managed property of type "Date and Time" it will present you a Date object with the timezone of your local pc.

Here is an example of how I retrieve my dates in my timezone:

Central Europe Daylight Time

If I would change my timezone on my local pc to central time US & Canada. The result of my date object will be different:

Central Daylight Time

Formatting the date is also very simple. You already retrieve a date object, so you do not have to cast it to a new date object. You should be able to do this:

var startdate = $getItemValue(ctx, 'RefinableDate05')
// In your HTML you could do this
startdate.value.format('MM-dd-yyyy hh:mm:ss tt');

Now there is a catch. You are using one of the predefined date and time managed properties (RefinableDate..), like you are using right now. By default these predefined managed properties are set to allow multiple values. This MP setting prevents the managed property of returning a date value, it will return a string instead (like this: "2015-02-28T23:00:00Z"). That is why you had to cast the inputValue to a date object in order to format it.

If you uncheck the Allow multiple values setting in the managed property, you would retrieve date objects instead of a string. Once you have done this, and a full crawl has ran, you should get date object instead of strings.

  • Elio thanks for your response, but it seems to have cut off at the last line "Another catch could be the timezone that is configured in the regional settings of your site. This could..." – Hayden Sinclair Nov 15 '15 at 22:13
  • Thanks, that's weird. What I wanted to say is that the regional settings of the site will also have impact on how the custom datetime fields get indexed. – Elio Struyf Nov 16 '15 at 9:33
1
var StartDate1 = new Date(StartDate.inputValue);  

//getTimezoneOffset() will give you time zone difference between the client machine local time and GMT/UTC in minutes.
//It will give positive value if you are behind UTC and negative value if you are ahead of UTC

var offset = StartDate1.getTimezoneOffset();
//Add the timezone difference 
StartDate1.setMinutes(StartDate1.getMinutes()+offset);
0

If you prefer plain Javascript without 3rd party Libs:

var startDateDTT = new Date ($getItemValue(ctx, "StartDate"));  
var startDateSTR = (startDateDTT != 'Invalid Date') ? (startDateDTT.getFullYear() + '-' + ("0" + (startDateDTT.getMonth() + 1)).slice(-2) + '-' + ("0" + startDateDTT.getDate()).slice(-2)) : '';

(this will format the date with YYYY-MM-DD)

If you prefer something more fancy, look at jquery dateFormat library:

https://github.com/phstc/jquery-dateFormat

  • Dave, you don't have to use third party libraries. You can make use of the format method that is available from the msajaxbundle.js file (is already loaded). Check my answer. – Elio Struyf Nov 17 '15 at 19:16

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.