WannabeCoder
WannabeCoder

Reputation: 57

Google script incorrect date conversion from string?

The following code in my Google script:

var checkin_date = new Date(customer.checkin); //customer.checkin format DD/MM/YYYY
Logger.log("%s", customer.checkin);
Logger.log("%s", checkin_date);

Gives me:

[18-10-28 23:28:06:662 PDT] 26/10/2018
[18-10-28 23:28:06:662 PDT] Mon Feb 10 00:00:00 GMT+05:30 2020

Which is wrong, why?

Upvotes: 0

Views: 686

Answers (1)

shabnam bharmal
shabnam bharmal

Reputation: 584

var customer="26/10/2018"
var dateArray=customer.split('/'); //[26,10,2018]

//new Date(year, month-1, day)
var checkin_date=new Date(dateArray[2],dateArray[1]-1,dateArray[0]);
checkin_date=Utilities.formatDate(new Date(checkin_date), Session.getScriptTimeZone(),         "dd/MM/yyyy");
Logger.log(checkin_date)

Hope this could help

Upvotes: 1

Related Questions