Welcome to MSDN Blogs Sign in | Join | Help

How do I get localization in JScript (JavaScript) in IE7?

I ran into an interesting problem with a Vista sidebar calendar gadget.  It seems that it is not exactly trivial to find out interesting NLS data like localized month names, first day of week, etc. from JScript.

Eventually I found a workaround for some of the data.  VBScript has MonthName, WeekDayName and WeekDay functions that can be used for some of the data I was looking for.  These can be wrapped in VBScript functions which JScript can then call from the same page.  I calculated FirstDayOfWeek information by using the WeekDay function for a known date.

These are the helpers that I ended up with:

<script language="VBscript">
' Helper functions so we can do this in JScript as well
Function MyMonthName(month)
   MyMonthName = MonthName(month)
End Function

Function MyAbbrevMonthName(month)
   MyAbbrevMonthName = MonthName(month, True)
End Function

Function MyDayName(day)
   MyDayName = WeekDayName(day)
End Function

Function MyAbbrevDayName(day)
   MyAbbrevDayName = WeekDayName(day, True)
End Function

' Get First Day of Week where 1 is Sunday, 2 is Monday, etc.
Function FirstDayOfWeek()
   Dim MyDate
   MyDate = DateSerial(2002, 9, 1) ' A Sunday
   FirstDayOfWeek = (9 - WeekDay(MyDate, 0))
   if FirstDayOfWeek = 8 then FirstDayOfWeek = 1
End Function

</script>

Unfortunately some items like Shortest Day Names and Month/Year patterns can't be retrieved this way, but I was able to solve most of my problem.  The attached CalendarData.html uses these helper functions to dump the values I was interested in using both VBScript and JScript.

I also embedded the HTML here, so here's the output, hope this is interesting:

Published Tuesday, September 05, 2006 4:08 PM by shawnste
Attachment(s): CalendarData.html

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Localized Date Strings

I just saw this interesting exchange on an internal discussion list, and thought I'd pass it on: From:...
Sunday, October 01, 2006 1:29 PM by Gadget Corner

# Software Information &raquo; Gadget Corner

Friday, January 25, 2008 12:09 PM by Software Information » Gadget Corner

# Desktop Computers &raquo; Gadget Corner

Thursday, March 20, 2008 4:45 AM by Desktop Computers » Gadget Corner

# Localized Date Strings | MS Tech News

Monday, October 27, 2008 5:55 PM by Localized Date Strings | MS Tech News

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker