0

I'm a little bit new to javascript (but not scripting in general). Does anyone know a good way to compare dates which use this formatting? Need it for 'google script' if statements.

if ('14 Jul 16 - 16.34.06' > '5 May 17 - 08.24.27');
{
 ;do this
}

;another example
if ('7 Oct 16 - 09.19.55' < '28 Nov 16 - 02.06.12')
{
 ;do this
}

Should I use moment.js perhaps? Seems a bit big.

Thanks for the help!

  • You will have to parse the string to an date object, but since the string you provided is non-standart you have to do it manually with String.split/splice method. [MDN Date Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). Then you can use ```>,<,===``` to compare both objects. – Lumpenstein Dec 09 '19 at 12:02
  • `const convert = dStr => { [ddmmmyy, hhmmss] = dStr.split(" - "); return new Date(\`${ddmmmyy} ${hhmmss.replace(/\./g,":")}\`);}` – mplungjan Dec 09 '19 at 12:44
  • i dont think const works for google script – Mikael Ellingsen Dec 09 '19 at 14:10
  • neither does template literals – Mikael Ellingsen Dec 09 '19 at 14:28
  • converted it to a working function like so: ```function convert(dStr) { if (dStr == null) return; date = dStr.split(' - '); day = date[0].split(' '); date[1] = date[1].replace(/\./g,':') output = new Date(day[0] + ' ' + day[1] + ' 20' + day[2] + ' ' + date[1]); //had to add 20 in front of year here :( //Logger.log(output); return output; }``` thanks again for the help! – Mikael Ellingsen Dec 11 '19 at 13:17

0 Answers0