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
Jennifer Bell
I think you can try something like this:
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">Convert2DateTime</SPAN><SPAN class="punctuation token">(</SPAN>date_text<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">var</SPAN> months <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"Jan"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Feb"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Mar"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Apr"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"May"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Jun"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Jul"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">6</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Aug"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">7</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Sep"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Oct"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">9</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Nov"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Dec"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">11</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> date_list <SPAN class="operator token">=</SPAN> <SPAN class="token function">Split</SPAN><SPAN class="punctuation token">(</SPAN>date_text<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" "</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> day_val <SPAN class="operator token">=</SPAN> <SPAN class="token function">Number</SPAN><SPAN class="punctuation token">(</SPAN>date_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> month_val <SPAN class="operator token">=</SPAN> months<SPAN class="punctuation token">[</SPAN>date_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> year_val <SPAN class="operator token">=</SPAN> <SPAN class="token function">Number</SPAN><SPAN class="punctuation token">(</SPAN>date_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> time_text <SPAN class="operator token">=</SPAN> date_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> time_list <SPAN class="operator token">=</SPAN> <SPAN class="token function">Split</SPAN><SPAN class="punctuation token">(</SPAN>time_text<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">":"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> hour_val <SPAN class="operator token">=</SPAN> <SPAN class="token function">Number</SPAN><SPAN class="punctuation token">(</SPAN>time_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> min_val <SPAN class="operator token">=</SPAN> <SPAN class="token function">Number</SPAN><SPAN class="punctuation token">(</SPAN>time_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> sec_val <SPAN class="operator token">=</SPAN> <SPAN class="token function">Number</SPAN><SPAN class="punctuation token">(</SPAN>time_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="token function">Date</SPAN><SPAN class="punctuation token">(</SPAN>year_val<SPAN class="punctuation token">,</SPAN> month_val<SPAN class="punctuation token">,</SPAN> day_val<SPAN class="punctuation token">,</SPAN> hour_val<SPAN class="punctuation token">,</SPAN> min_val<SPAN class="punctuation token">,</SPAN> sec_val<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">// var datetext = $feature.Date;</SPAN> <SPAN class="keyword token">var</SPAN> date_text <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Mon Sep 17 13:11:06 +0000 2018"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="token function">Convert2DateTime</SPAN><SPAN class="punctuation token">(</SPAN>date_text<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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:
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);
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);
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.
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.
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.
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,
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime s <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Fri Jun 29 08:18:42 +0000 2018"</SPAN> d <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>strptime<SPAN class="punctuation token">(</SPAN>s<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'%a %b %d %H:%M:%S %z %Y'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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).
<SPAN class="comment token">// Date Format</SPAN> <SPAN class="comment token">// Fri Jun 29 08:18:42 +0000 2018</SPAN> <SPAN class="comment token">// Year</SPAN> <SPAN class="keyword token">var</SPAN> yearNum <SPAN class="operator token">=</SPAN> <SPAN class="token function">Right</SPAN><SPAN class="punctuation token">(</SPAN>$feature<SPAN class="punctuation token">.</SPAN>Date<SPAN class="punctuation token">,</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Month</SPAN> <SPAN class="keyword token">var</SPAN> monthText <SPAN class="operator token">=</SPAN> <SPAN class="token function">Mid</SPAN><SPAN class="punctuation token">(</SPAN>$feature<SPAN class="punctuation token">.</SPAN>Date<SPAN class="punctuation token">,</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> monthNum <SPAN class="operator token">=</SPAN> <SPAN class="token function">When</SPAN><SPAN class="punctuation token">(</SPAN>monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Jan'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Feb'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Mar'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Apr'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'May'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Jun'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Jul'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">6</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Aug'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Sep'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Oct'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">9</SPAN><SPAN class="punctuation token">,</SPAN> monthText<SPAN class="operator token">==</SPAN><SPAN class="string token">'Nov'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Day</SPAN> <SPAN class="keyword token">var</SPAN> dayNum <SPAN class="operator token">=</SPAN> <SPAN class="token function">Mid</SPAN><SPAN class="punctuation token">(</SPAN>$feature<SPAN class="punctuation token">.</SPAN>Date<SPAN class="punctuation token">,</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Time</SPAN> <SPAN class="keyword token">var</SPAN> timeNum <SPAN class="operator token">=</SPAN> <SPAN class="token function">Right</SPAN><SPAN class="punctuation token">(</SPAN>$feature<SPAN class="punctuation token">.</SPAN>Date<SPAN class="punctuation token">,</SPAN><SPAN class="number token">19</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Hour</SPAN> <SPAN class="keyword token">var</SPAN> hourNum <SPAN class="operator token">=</SPAN> <SPAN class="token function">Left</SPAN><SPAN class="punctuation token">(</SPAN>timeNum<SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Minute</SPAN> <SPAN class="keyword token">var</SPAN> minuteNum <SPAN class="operator token">=</SPAN> <SPAN class="token function">Mid</SPAN><SPAN class="punctuation token">(</SPAN>timeNum<SPAN class="punctuation token">,</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Second</SPAN> <SPAN class="keyword token">var</SPAN> secondNum <SPAN class="operator token">=</SPAN> <SPAN class="token function">Mid</SPAN><SPAN class="punctuation token">(</SPAN>timeNum<SPAN class="punctuation token">,</SPAN><SPAN class="number token">6</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="token function">Date</SPAN><SPAN class="punctuation token">(</SPAN>yearNum<SPAN class="punctuation token">,</SPAN>monthNum<SPAN class="punctuation token">,</SPAN>dayNum<SPAN class="punctuation token">,</SPAN>hourNum<SPAN class="punctuation token">,</SPAN>minuteNum<SPAN class="punctuation token">,</SPAN>secondNum<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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?
Did you try some of the options for Arcade listed here?
Date Functions | ArcGIS for Developers
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.