Text to Date using Arcade Expressions

10511
12
Jump to solution
10-05-2018 12:39 PM
by Anonymous User
Not applicable

Hi all,

Could anyone assist me in converting the following String type field

Mon Sep 17 13:11:06 +0000 2018

into a Date type field?

Thanks,

Gee

Convert String to Date

Jennifer Bell

12 Replies
AndrewL
Occasional Contributor II

Thanks this worked for me as well. Is there a way to tell it that this is UTC time, and that it should adjust to whatever time zone the user is in when displaying?

For example, my expression converts text (2019-03-19 20:00:00, UTC) to 
Tue Mar 19 2019 20:00:00 GMT-0500 (Central Daylight Time)
even though it doesn't convert it to CDT.

AndrewL
Occasional Contributor II

There is a "ToLocal" function within Arcade expressions that I was able to use.

deleted-user-8TJ6jZDO1Rfr
New Contributor III

I worked through this issue today and was able to use both the codes in this thread to make my situation work. I had a date in a string format that had inconsistent amount of characters e.g. 1/2/2016; 6/22/2017; 12/12/2018. Using a split array and a dictionary worked well.  Thanks Xander Bakker and Geethaka Fernando

My code:

var date_text = $feature["START_DATE"]

function Convert2Date(date_text){

     // Month List
     var months = {"1": 0, "2": 1, "3": 2, "4": 3, "5": 4, "6": 5, "7": 6, "8": 7, "9": 8, "10": 9, "11": 10, "12": 11};

     

     // Date List
     var date_list = Split(date_text, '/')

     // Year
     var yearNum = Number(date_list[2]);

     // Month
     var monthNum = months[date_list[0]];

     // Day
     var dayNum = Number(date_list[1]);

     return Date(yearNum,monthNum,dayNum);
}

return Convert2date(date_text);