I have a form set up that 1 person fills. The form asks for multiple things, including a date using date picker. The date has 2 main uses. I use the formatDate to convert it to "YY" and "dd-MM-YY", which both serves different purposes.
The main problem I faced today is when I saw an entry, the date inputted was 31 December 2023, and the log of date inputted is showing as 31-12-23, however, once I'm passing it through the Utilities.formatDate, it is automatically converting as 31-12-24. The date and the month are ok, but the year is not.
shows the formatDate funtion Detail of the function
The code I'm using is as follows: (I also tried changing the time zone to "GMT+5", but the result is the same)
function test() {
var sdate = responses.getItemResponses()[3].getResponse();//date from the form
Logger.log(sdate);
Logger.log(Utilities.formatDate(new Date(sdate), Session.getScriptTimeZone(), "YY-MM-dd"));
Logger.log(Utilities.formatDate(new Date("December 30, 2023"), Session.getScriptTimeZone(), "YY-MM-dd"));
Logger.log(new Date(sdate).getFullYear().toString().slice(-2));
}
Here is the log result:
3:23:18 PM Notice Execution started
3:23:14 PM Info 2023-12-31
3:23:14 PM Info 24-12-31
3:23:14 PM Info 23-12-30
3:23:14 PM Info 23
3:23:21 PM Notice Execution completed
I also changed the manually written date from 30-12-23 to 31-12-23, but even then, the log shows as 31-12-24:
The code:
Logger.log(Utilities.formatDate(new Date("December 31, 2023"), Session.getScriptTimeZone(), "YY-MM-dd"));
The log:
3:29:03 PM Info 24-12-31
Please help as soon as possible. Some year end tasks are pending because of it!
Issue:
Ystands forWeek Year. If the the date parsed falls on a week that ends in 2024, then that date's week year is 2024. For week 1 of 2024, all dates from Dec 31 2023(Sunday) to Jan 6, 2024(Saturday) belongs to week year 2024.References:
https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#week_year
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Solution:
Use
yinstead.