- You want to parse the string value of
yyyy-mm-dd hh:MM:ss timezone
as a date object.
If my understanding is correct, how about this answer? I think that there are several answers for your situation. So please think of this as one of them.
When I saw the format, I thought that RFC3339 is close to the format. So how about the following flow?
- Split the value by
which is a space.
- Join 1st and 2nd element of the splited value by "T".
- Add last element to the joined value.
When 2018-09-21 16:00:00 -0500
is used for this flow, the value is converted to 2018-09-21T16:00:00-0500
. This can be used as the RFC3339 type. By this flow, yyyy-mm-dd hh:MM:ss timezone
can be parsed as the date object.
Sample script:
Pattern 1:
The sample script which reflected above flow is as follows.
var str = "2018-09-21 16:00:00 -0500"; // yyyy-mm-dd hh:MM:ss timezone
var ar = str.split(" "); // Added
var str = ar.slice(0, 2).join("T") + ar[2]; // Added: Here str is "2018-09-21T16:00:00-0500".
var d = new Date(str);
var days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
var day = days[d.getDay()];
var hh = d.getHours();
var mm = d.getMinutes()
if(hh < 10) {hh = "0"+hh}
if(mm < 10) {mm = "0"+mm}
Logger.log(day+" "+hh+mm) // Added
return day+" "+hh+mm
Pattern 2:
As an another pattern, how about this script? In Google Apps Script, you can also use Utilities.formatDate()
.
var str = "2018-09-21 16:00:00 -0500";
var ar = str.split(" ");
var r = ar.slice(0, 2).join("T") + ar[2];
var d = new Date(r);
var res = Utilities.formatDate(d, Session.getScriptTimeZone(), "E hhmm").toUpperCase();
Logger.log(res)
Note:
- If the format is different from
yyyy-mm-dd hh:MM:ss timezone
, this script might not be able to be used. Please be careful about this.
Reference:
If I misunderstand your question, I'm sorry.