Text to Date using Arcade Expressions

10546
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

1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

I think you can try something like this:

function Convert2DateTime(date_text){
    var months = {"Jan": 0, "Feb": 1, "Mar": 2,
                  "Apr": 3, "May": 4, "Jun": 5,
                  "Jul": 6, "Aug": 7, "Sep": 8,
                  "Oct": 9, "Nov": 10, "Dec": 11};

    var date_list = Split(date_text, " ");
    var day_val = Number(date_list[2]);
    var month_val = months[date_list[1]];
    var year_val = Number(date_list[5]);
    var time_text = date_list[3];
    var time_list = Split(time_text, ":");
    var hour_val = Number(time_list[0]);
    var min_val = Number(time_list[1]);
    var sec_val = Number(time_list[2]);
    return Date(year_val, month_val, day_val, hour_val, min_val, sec_val);
}

// var datetext = $feature.Date;
var date_text = "Mon Sep 17 13:11:06 +0000 2018";
return Convert2DateTime(date_text);

I tested with the value "Mon Sep 17 13:11:06 +0000 2018" (see line 20). You should however, point to a attribute field like the example commented on line 19. It will assume the time is expressed in your local time zone:

View solution in original post

12 Replies
DanPatterson_Retired
MVP Emeritus

Did you try some of the options for Arcade listed here?

Date Functions | ArcGIS for Developers 

XanderBakker
Esri Esteemed Contributor

I think you can try something like this:

function Convert2DateTime(date_text){
    var months = {"Jan": 0, "Feb": 1, "Mar": 2,
                  "Apr": 3, "May": 4, "Jun": 5,
                  "Jul": 6, "Aug": 7, "Sep": 8,
                  "Oct": 9, "Nov": 10, "Dec": 11};

    var date_list = Split(date_text, " ");
    var day_val = Number(date_list[2]);
    var month_val = months[date_list[1]];
    var year_val = Number(date_list[5]);
    var time_text = date_list[3];
    var time_list = Split(time_text, ":");
    var hour_val = Number(time_list[0]);
    var min_val = Number(time_list[1]);
    var sec_val = Number(time_list[2]);
    return Date(year_val, month_val, day_val, hour_val, min_val, sec_val);
}

// var datetext = $feature.Date;
var date_text = "Mon Sep 17 13:11:06 +0000 2018";
return Convert2DateTime(date_text);

I tested with the value "Mon Sep 17 13:11:06 +0000 2018" (see line 20). You should however, point to a attribute field like the example commented on line 19. It will assume the time is expressed in your local time zone:

DanPatterson_Retired
MVP Emeritus

Xander Bakker‌  Xander, even though Arcade is cross-platform, what part of the platform chain necessitates having to declare your variables.  what is it based on and why?

XanderBakker
Esri Esteemed Contributor

Hi Dan Patterson , Arcade is based on JavaScript and hence the declarations. It is an abstract of JavaScript and therefore the possibilities are limited (compared to JavaScript). It does have geometry functions which are quite cool. The reason for Arcade is that is it more "webby" than Python and closer to what is used to develop for instance ArcGIS Online. Therefor, it is easier to enable in web maps than Python.

by Anonymous User
Not applicable

Thanks Xander Bakker‌ and Dan Patterson‌ for the helpful information.

I managed to get this working with the following script (just forgot to post it online).

// Date Format
// Fri Jun 29 08:18:42 +0000 2018

// Year
var yearNum = Right($feature.Date,4);

// Month
var monthText = Mid($feature.Date,4,3);
var monthNum = When(monthText=='Jan',0, monthText=='Feb',1, monthText=='Mar',2, monthText=='Apr',3, monthText=='May',4, monthText=='Jun',5, monthText=='Jul',6, monthText=='Aug',7, monthText=='Sep',8, monthText=='Oct',9, monthText=='Nov',10,11);

// Day
var dayNum = Mid($feature.Date,8,2);

// Time
var timeNum = Right($feature.Date,19);

// Hour
var hourNum = Left(timeNum,2);

// Minute
var minuteNum = Mid(timeNum,3,2);

// Second
var secondNum = Mid(timeNum,6,2);

return Date(yearNum,monthNum,dayNum,hourNum,minuteNum,secondNum);
by Anonymous User
Not applicable

But, at the end I just updated the python script that was ingesting data into the ArcGIS online feature layer.

Thanks again for the help.

Cheers,

Gee

from datetime import datetime

s = "Fri Jun 29 08:18:42 +0000 2018"
d = datetime.strptime(s, '%a %b %d %H:%M:%S %z %Y')
print(d)‍‍‍‍‍
DanPatterson_Retired
MVP Emeritus

nice and simple as it should be

XanderBakker
Esri Esteemed Contributor

I totally agree! When I searched for an answer I was looking from something similar to strptime in Python and surprised that there is no such thing in Arcade. Always better to go directly to the source and change it there. In this case I totally agree with  Dan_Patterson it is nice and simple.

XanderBakker
Esri Esteemed Contributor

Cool! Great that you solved it by yourself. It is always good to post back the solution, because this way others can benefit from what you have found out.