So this is a super quick note that I'll probably remember anyway but just in case, I'm writing this article so I don't spend time researching it later.
Why?
I'm synchronizing Xero Invoices with Zoho CRM Invoices and noticed that Xero stores its dates in Unix Timestamps.
How?
We're going to filter out the unix seconds from the date provided by Xero then apply a toTime() function to it.
v_XeroTime="/Date(1586995200000+0000)/"; v_StartIndex = v_XeroTime.indexOf("(")+1; v_EndIndex = v_XeroTime.indexOf("+", v_StartIndex); v_UnixSeconds = v_XeroTime.subString(v_StartIndex, v_EndIndex); info v_UnixSeconds.toLong().toTime(); // yields 15-Apr-2020 17:00:00
- v_XeroTime="/Date(1586995200000+0000)/";
- v_StartIndex = v_XeroTime.indexOf("(")+1;
- v_EndIndex = v_XeroTime.indexOf("+", v_StartIndex);
- v_UnixSeconds = v_XeroTime.subString(v_StartIndex, v_EndIndex);
- info v_UnixSeconds.toLong().toTime();
- // yields 15-Apr-2020 17:00:00
Chained
v_XeroTime="/Date(1586995200000+0000)/"; v_ZohoTime = v_XeroTime.subString(v_XeroTime.indexOf("(")+1, v_XeroTime.indexOf("+")).toLong().toTime(); // yields 15-Apr-2020 17:00:00
- v_XeroTime="/Date(1586995200000+0000)/";
- v_ZohoTime = v_XeroTime.subString(v_XeroTime.indexOf("(")+1, v_XeroTime.indexOf("+")).toLong().toTime();
- // yields 15-Apr-2020 17:00:00
Using a regular expression
v_XeroTime="/Date(1586995200000+0000)/"; v_ZohoTime = v_XeroTime.replaceAll("[^0-9]", " ", false).trim().toList(" ").get(0).toLong().toTime(); // yields 15-Apr-2020 17:00:00
- v_XeroTime="/Date(1586995200000+0000)/";
- v_ZohoTime = v_XeroTime.replaceAll("[^0-9]", " ", false).trim().toList(" ").get(0).toLong().toTime();
- // yields 15-Apr-2020 17:00:00
Additional
- The reverse (date string to unix seconds) can be done using either of the following:copyraw
v_MyDate = "2020-04-01 12:34:56"; v_UnixSeconds = v_MyDate.toTime().toLong(); info v_UnixSeconds; // or v_UnixSeconds = v_MyDate.unixEpoch(); info v_UnixSeconds;
- v_MyDate = "2020-04-01 12:34:56";
- v_UnixSeconds = v_MyDate.toTime().toLong();
- info v_UnixSeconds;
- // or
- v_UnixSeconds = v_MyDate.unixEpoch();
- info v_UnixSeconds;
- Error: Argument type mismatch -Found 'TEXT' but Expected 'Long' // Solution: Convert your string into a date.
Source(s)