vrijdag 6 december 2019

Get weekday name from a date in the language of your choice

Until I read this blog  about getting a weekday name from a date, I used a summer time/winter time script I wrote a few months ago.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var GMT_TIMES = [[2019,[2019,02,31],[2019,9,27]],[2020,[2020,02,29],[2020,9,25]]];

function gmtSummerAndWinterTime(){
  
  var today = new Date(); 
  var thisYear = today.getFullYear(); 
  var todayNumber = today.getTime();
  
  for(var g=0;g<GMT_TIMES.length;g++){
   
    var gmtTijd = GMT_TIMES[g]; 
    var dateSummerTimeNumber = new Date(GMT_TIMES[1][0],GMT_TIMES[1][1],GMT_TIMES[1][2]).getTime(); 
    var dateWinterTimeNumber = new Date(GMT_TIMES[2][0],GMT_TIMES[2][1],GMT_TIMES[2][2]).getTime();
         
    if(GMT_TIMES[0] == thisYear && dateSummerTimeNumber < todayNumber && todayNumber < dateWinterTimeNumber){
      var gmt = "GMT+2"; 
      return gmt;
    }
    else{
    
      gmt = "GMT+1";
      return gmt
    }
  }
}

The Session thing is so much easier.

'Problem' with formatDate(date,timeZone,format)  in combination with  SimpleDateFormat class is that it is in English.  There are people using other languages than English.

The Google Language Service could be used to solve the 'problem'.

I prefer the following 'solution'.


1
2
3
4
5
6
7
8
function getWeekDayName(){
  var weekdayNames = ["zondag","maandag","dinsdag","woensdag","donderdag","vrijdag","zaterdag"];  
  var today = new Date();
  var timeZone = Session.getScriptTimeZone(); 
  var weekdayNumber = Utilities.formatDate(today,timeZone,"u");
  var weekdayName = weekdayNames[weekdayNumber];
  Logger.log('Weekday name is: ' + weekdayName);
}


Resources:
Get weekday name from a date,  Phil Bainbridge, 2 december 2019

Geen opmerkingen:

Een reactie posten