Use arcade to convert a number to a date

130
3
Jump to solution
3 weeks ago
MaryroseKulick
New Contributor II

Hello!

I'm working on displaying fields in a list in a dashboard, and I'm struggling to accomplish this goal:

I have a number field representing a date formatted as: 7012021

I need it to read as : July 01 2021 or 7/01/2021

Any tips are appreciated! I am new to arcade, so I'm having trouble figuring out the right function to use. The replace function almost worked, but it's not quite what I need.

Thank you!

 

 

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Regular Contributor

Building off this, you can use left, right, and count together to get the various date parts out regardless of whether the leading zero is present or not.

By returning a true Date object, you can then modify that to output to any format you like, so if you prefer the long date style of "July" instead of "7", you can do that.

var dateNum = Text(7012021)

var y = right(dateNum, 4)
var d = left(right(dateNum, 6), 2)
var m = left(dateNum, 8 - count(dateNum)) - 1

// month is returned as the number - 1, as Date function takes month as a 0-indexed value, i.e., January = 0

var dt = Date(y, m, d)

return Text(dt, 'MMMM DD Y')

jcarlson_0-1632339240785.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
3 Replies
DavidPike
MVP Frequent Contributor

I'm not sure of all the different cases you may have but this is a simplistic way, assuming that the possible formats remain consistent, i.e. the only change could be an additional digit at the start.

NB I used your value for testing, obviously change 7012021 to $feature.Value or whatever.

var dateNum = 7012021

if (Count(Text(dateNum)) == 7) {
    return Left(dateNum, 1) + '/' + Mid(dateNum, 1, 2) + '/' + Mid(dateNum, 3, 4)
}
else {
    return Left(dateNum, 2) + '/' + Mid(dateNum, 2, 2) + '/' + Mid(dateNum, 4, 4)
}

 

jcarlson
MVP Regular Contributor

Building off this, you can use left, right, and count together to get the various date parts out regardless of whether the leading zero is present or not.

By returning a true Date object, you can then modify that to output to any format you like, so if you prefer the long date style of "July" instead of "7", you can do that.

var dateNum = Text(7012021)

var y = right(dateNum, 4)
var d = left(right(dateNum, 6), 2)
var m = left(dateNum, 8 - count(dateNum)) - 1

// month is returned as the number - 1, as Date function takes month as a 0-indexed value, i.e., January = 0

var dt = Date(y, m, d)

return Text(dt, 'MMMM DD Y')

jcarlson_0-1632339240785.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
MaryroseKulick
New Contributor II

This worked great in the dashboard environment! My dates look beautiful now. :) Thank you!

0 Kudos