Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a date that I want to display to the user, and the format should be dynamic and based on the user. If I do this I get a format that doesn't fit every user.

var prettyDate = originalDate.format('dd MMM yyy, hh:ss');

For example, in Sweden we would like it displayed as 2014-03-04 but another country would want it as 4/3 2014 So instead of explicitly writing the format in the code, I want to get the preferred date format for the current user. My idea was that I could maybe use the culture found in _spPageContextInfo, or something in SP.DateTimeUtil, but I can't find anything.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Microsoft Ajax library also contains Date.localeFormat Function that formats a date by using the current culture.

Example:

var prettyDate = originalDate.localeFormat('dd MMM yyy, hh:ss');
console.log(prettyDate);
share|improve this answer
    
But that still means I have to put in a format string and I don't want to do that. What I want is to use the same rendering of the date as used by SharePoint default, for example in a list –  stinaq Mar 4 at 9:58
3  
@stinaq did you see the example dateVar.localeFormat(Sys.CultureInfo.CurrentCulture.dateTimeFormat.ShortDatePat‌​tern);? As a last resort perhaps look at momentjs.com –  eirikb Mar 4 at 10:15
    
Now I saw it! Thank you very much! –  stinaq Mar 4 at 10:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.