Reputation: 307
I have a start date that looks like this:
var startDate = "Mon Jun 30 2014 00:00:00 GMT-0400 (EDT)";
And I am trying to get it formatted into this:
var endDate = "2014-06-30T00:00:00-04:00";
I have gotten it to semi-format correctly using the toISOString() method:
var formattedDate = new Date(startDate);
formattedDate = formattedDate.toISOString();
Which formats it into "2014-06-30T04:00:00.000Z". This is close to what I need, but I was wondering if there was built in method to format it into the '-04:00' format? Or do I need to split my string into parts and mend it back together in the right format?
Also I am working out of Google Apps Script, which is ES5, and am trying to avoid jQuery if possible.
Upvotes: 0
Views: 1071
Reputation: 147503
You can just reformat the string. The format in the OP is consistent with the format specified for Date.prototype.toString in ECMAScript 2018, so you can even reformat that wiht the same function:
// Convert string in DDD MMM DD YYYY HH:mm:ss ZZ format to
// YYYY-MM-DDTHH:mm:ssZZ format
function formatDateString(s) {
var m = {Jan:'01',Feb:'02',Mar:'03',Apr:'04',May:'05',Jun:'06',
Jul:'07',Aug:'08',Sep:'09',Oct:'10',Nov:'11',Dec:'12'};
var b = s.split(' ');
return `${b[3]}-${m[b[1]]}-${b[2]}T${b[4]}${b[5].slice(-5,-2)}:${b[5].slice(-2)}`;
}
console.log(formatDateString("Mon Jun 30 2014 00:00:00 GMT-0400 (EDT)"));
// Not recommended, but it will format any date where its
// toString method is consistent with ECMAScript 2018
console.log(formatDateString(new Date().toString()));
Upvotes: 0
Reputation: 8974
Google Apps Script (GAS) has a built-in utility method you can leverage to format dates:
Its based on Java's SimpleDateFormat
class.
To format the date to meet your requirements the following should suffice:
var date = new Date("Mon Jun 30 2014 00:00:00 GMT-0400 (EDT)");
var formattedDate = Utilities.formatDate(
date,
Session.getScriptTimeZone(),
"yyyy-MM-dd'T'HH:mm:ssXXX"
);
Note: You may need to set your script's timezone from the GAS GUI menu via:
File->Project Properties->Info
Upvotes: 2
Reputation: 6953
I completely agree with @JordanRunning 's comment.
Still, out of curiosity I quickly put together a way to get your desired format:
// two helper functions
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
function getOffset(offset) {
if(offset<0) {
wminutes = 0 - offset;
var posneg = "-";
} else {
wminutes = offset;
var posneg = "+";
}
var hours = pad(Math.floor(wminutes/60));
var minutes = pad(wminutes % 60);
return posneg+hours+":"+minutes;
}
// the actual format function
Date.prototype.toMyFormat = function() {
var format = this.getUTCFullYear() +
'-' + pad(this.getMonth() + 1) +
'-' + pad(this.getDate()) +
'T' + pad(this.getHours()) +
':' + pad(this.getMinutes()) +
':' + pad(this.getSeconds());
timezoneOffset = getOffset(this.getTimezoneOffset());
format += timezoneOffset;
return format;
}
// usage:
console.log(new Date().toISOString());
// "2018-11-16T20:53:11.365Z"
console.log(new Date().toMyFormat());
// "2018-11-16T21:53:11-01:00"
Upvotes: 0