Select to view content in your preferred language

Date format in Arcade gives wrong results

2730
6
Jump to solution
02-11-2021 02:20 AM
Labels (1)
kirken
by
Occasional Contributor

Hi! I'm trying to understand how to generate dates using Arcade but it's quite confusing. 

1. Why does this code  

return Date(2020,11,02);

 return a result : 01.12.2020 22:00:00 ???? 

I get very weird results when trying to use this function 😞

2. Then again, when trying to convert from string I get better results:

var year = Year(`20201102`); //02.11.2020 -> November 2, 2020
var month = Month(`20201102`);
var day = Day(`20201102`);
return Date(year, month, day, 0, 0, 0, 0);

returns 01.11.2020 22:00:00 which is almost correct, but still 1 day less than correct

3. How to get rid from minutes - hours- second ?

very confused

thanks

0 Kudos
2 Solutions

Accepted Solutions
KenBuja
MVP Esteemed Contributor

In addition to @DanPatterson answer, when you create a date using year, month, day, the month is 0-based (0=January and 11=December)

View solution in original post

XanderBakker
Esri Esteemed Contributor

Hi @kirken ,

@KenBuja answered your first question indicating that the month is 0 based (November would be represented by 10)

For the second question, I notice a couple of things. The Year, Month, and Day functions according to the documentation require a date to extract these values. You provide a string. Also, you used the names of functions for your variables and you should be careful about that. The fact that you got a result baffles me a bit, but I would not recommend taking this road. 

If you have a text that represents a date, you will probably have to extract the different parts, convert them to numbers and then provide these numbers to the Date function correcting the month value for being 0-based.

var datetext = '20201102';
var y = Number(Left(datetext, 4));
var m = Number(Mid(datetext, 4, 2));
var d = Number(Right(datetext, 2));
Console(y);
Console(m);
Console(d);
return Date(y, m-1, d);

 

Unfortunately, Arcade does not have a strptime() function like in Python which can convert a string to date using a provided patterns... (and yes @DanPatterson in Python this would be a lot easier).

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

Date Functions | ArcGIS for Developers

Date Functions | ArcGIS for Developers

Are you using local time or UTC time.  You first example is a date format issue/choice offset by a small time difference.  So check both and the format links as well


... sort of retired...
0 Kudos
KenBuja
MVP Esteemed Contributor

In addition to @DanPatterson answer, when you create a date using year, month, day, the month is 0-based (0=January and 11=December)

XanderBakker
Esri Esteemed Contributor

Hi @kirken ,

@KenBuja answered your first question indicating that the month is 0 based (November would be represented by 10)

For the second question, I notice a couple of things. The Year, Month, and Day functions according to the documentation require a date to extract these values. You provide a string. Also, you used the names of functions for your variables and you should be careful about that. The fact that you got a result baffles me a bit, but I would not recommend taking this road. 

If you have a text that represents a date, you will probably have to extract the different parts, convert them to numbers and then provide these numbers to the Date function correcting the month value for being 0-based.

var datetext = '20201102';
var y = Number(Left(datetext, 4));
var m = Number(Mid(datetext, 4, 2));
var d = Number(Right(datetext, 2));
Console(y);
Console(m);
Console(d);
return Date(y, m-1, d);

 

Unfortunately, Arcade does not have a strptime() function like in Python which can convert a string to date using a provided patterns... (and yes @DanPatterson in Python this would be a lot easier).

kirken
by
Occasional Contributor

Thanks a lot, it's clear now!!!!

0 Kudos
FranEvanisko
Emerging Contributor

I have no idea why someone would decide to numerate months between 0 and 11 rather than between 1 and 12.  How does this make sense.  I guess it is not a big deal after you become familiar with this feature,  however it is very error-prone.  

DonaldMarutaNHS
Emerging Contributor

I have just fallen foul of this, and spent the last hour or so, going over my code wondering why I'm not getting the correct results for my calculations!!!