0

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?

3
  • The date is in string format so you can split the string by '/' and create a date with new Date again by passing dd,mm,yyy. Commented Oct 29, 2018 at 6:45
  • I am confused can you show a little example pls. Commented Oct 29, 2018 at 6:48
  • There are specific date string formats Date recognizes see w3schools.com/js/js_date_formats.asp. Or as noted above split it into 3 numbers and pass to Date(yy,mm-1,dd) Commented Oct 29, 2018 at 16:05

1 Answer 1

1
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

Sign up to request clarification or add additional context in comments.

2 Comments

But Logger.log(Utilities.formatDate(new Date(checkin_date), Session.getScriptTimeZone(),"MMM").toUpperCase()); still gives me FEB :(
var checkin_date=new Date(dateArray[2],dateArray[1]-1,dateArray[0]); Logger.log(Utilities.formatDate(new Date(checkin_date), Session.getScriptTimeZone(),"MMM").toUpperCase()); Try now. You were adding utilities.format to a date second time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.