<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Casting dates to and from JSON</title><link>https://community.appian.com/discussions/f/data/13730/casting-dates-to-and-from-json</link><description>Hi, 
 So when Appian writes dates to Json, it cannot cast those values correctly if it reads them back. 
 
 Do I have to manually alter the string in order to reorder the date from &amp;quot;YYYY-MM-DDZ&amp;quot; to &amp;quot;DD-MM-YYYYZ&amp;quot; before I run from Json? 
 Is there a reason</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/84226?ContentTypeID=1</link><pubDate>Mon, 02 Aug 2021 05:16:03 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:15473485-c1ca-4385-9cfe-3a18067587d8</guid><dc:creator>fosterc0001</dc:creator><description>&lt;p&gt;JSON does not know anything about dates. What .NET does is a non-standard hack/extension. The problem with &lt;a href="http://net-informations.com/jq/iq/jdate.htm"&gt;dates in JSON&lt;/a&gt; and really JavaScript in general &amp;ndash; is that there&amp;#39;s no equivalent literal representation for dates. In JavaScript following Date constructor straight away converts the milliseconds since 1970 to Date as follows:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;var jsonDate = new Date(1297246301973);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Then let&amp;#39;s convert it to js format:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;var date = new Date(parseInt(jsonDate.substr(6)));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor .&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/68334?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 09:19:02 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:9cecf67a-dd5c-4c1f-a309-f077fa735d5c</guid><dc:creator>osuvorov</dc:creator><description>&lt;p&gt;Hi &lt;a href="/members/jonathanb0001"&gt;jonathanb0001&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can try this approach (work for 17.4 at least):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="json"&gt;with(
  local!listOfDates: {now(),today()-1,today()+7},
	local!jsonString: a!toJson(
	  {
		/* ... */
		dateValues: cast(&amp;#39;type!Text?list&amp;#39;,
		  a!forEach(local!listOfDates,text(fv!item,&amp;quot;mm/dd/yyyy&amp;quot;))
		)
		/* ... */
	  }
	),
	local!jsonObject: a!fromJson(local!jsonString),
	local!dateValues: cast(&amp;#39;type!Date?list&amp;#39;,
	  property(local!jsonObject,&amp;quot;dateValues&amp;quot;,null)),
	local!dateValues
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62615?ContentTypeID=1</link><pubDate>Wed, 14 Nov 2018 05:04:58 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:a02ca057-0a6c-4dc5-b59b-006e724b6e2a</guid><dc:creator>manisht209</dc:creator><description>&lt;p&gt;Regex functions do come from a plugin -&amp;nbsp;&lt;a href="https://forum.appian.com/suite/tempo/records/item/lMBCLGOdlMUpdGVqW3dQaIKmclBmvvNEj8vu_cjb7T-5YiPr4Fu8ly5Yj1s09uenE4RYzA8zKyx7eiUhemjLnMWmh9flkoBj4U4HmAkkPRpCPt5Lw/view/summary"&gt;shared component&lt;/a&gt;&amp;nbsp;which is cloud approved as well.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;I checked the previous release documents and can trace back the torecord() to 17.1 the oldest supported version. But frankly speaking, don&amp;#39;t remember when was this introduced. Seems it had always been there. Something odd with your installation.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;datetext&amp;nbsp;I am assuming you want to convert the datetime to a string first - this will give you a string notation of user&amp;#39;s local time. then while converting it back to datetime you need to honor the GMT offset. parse the string and get this offset, use local on top of it. Appian wouldn&amp;#39;t automatically convert this from a string. Then you have to make sure consumers always pass you the string in an agreed&amp;nbsp;format (that&amp;#39;s&amp;nbsp;why I&amp;nbsp;thought of&amp;nbsp;XML&amp;nbsp;standard as Appian seems to be doing this anyway).&lt;br /&gt;&lt;br /&gt;And if you have this agreement in place, you don&amp;#39;t need regex at all (as suggested in the original post :) ).&lt;br /&gt;Regex is just a check before attempting a conversion if your agreement on a format is not in place or there is a potential of this being consumed at a large scale (at the enterprise level). You don&amp;#39;t&amp;nbsp;want to rely on consumer always passing the data in the correct format. Otherwise, you can ignore the regex code and just convert it to datetime. if the data is not proper your conversion will fail.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62562?ContentTypeID=1</link><pubDate>Tue, 13 Nov 2018 08:32:08 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:a7994097-bf5c-4c2d-b324-45e9f55a57cb</guid><dc:creator>jonathanb0001</dc:creator><description>&lt;p&gt;Huh thats really interesting, however I&amp;#39;m having a couple of problems.&lt;/p&gt;
&lt;p&gt;Firstly, I think that regex function is a plugin rather than Appian basic so it seems questionable that it would be Appians recommendation. Although they might reccomend the same just with the out of the box text functions.&lt;/p&gt;
&lt;p&gt;Secondly my Appian installation code suggestor thingy does not suggest that I use torecord() ever and if I manually enter it, I&amp;#39;m not given any details in that little box that usually contains a description and parameter. Given that I can see that Appian has documentation for this in 18.3 I assume this is a problem with my installation. (It does function it just acts like the function is meant to be hidden).&lt;/p&gt;
&lt;p&gt;And finally,&amp;nbsp;if we cannot handle CDTs and have to craft a custom parser rule why not just use datetext? I assume that it would be less expensive.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62548?ContentTypeID=1</link><pubDate>Tue, 13 Nov 2018 04:01:08 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:7d0743bf-ffb5-4c91-a082-36937e592d90</guid><dc:creator>manisht209</dc:creator><description>&lt;p&gt;I would suggest adhere to the XML standard for date time as while generating the JSON from Appian, Appian would do so as well. Then &amp;nbsp;while converting it back from JSON you need to convert it to xml format and then cast to datetime.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ToJson :&lt;br /&gt;&lt;pre class="ui-code" data-mode="html"&gt;a!toJson(
  value: {
    datestamp: now()
  }
)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;FromJSON :&lt;br /&gt;&lt;pre class="ui-code" data-mode="html"&gt;load(
  local!jsonData: a!fromJson(
    &amp;quot;{&amp;quot;&amp;quot;datestamp&amp;quot;&amp;quot;:&amp;quot;&amp;quot;2018-11-13T04:54:46.029Z&amp;quot;&amp;quot;}&amp;quot;
  ).datestamp,
  if(
    regexmatch(
      &amp;quot;^([0-9]+\d[-][0-9]\d[-][0-9]\d[T][0-9]\d[:][0-9]\d[:][0-9]\d[\.][0-9]+[Z]+)$&amp;quot;,
      local!jsonData
    ),
    torecord(
      toxml(
        local!jsonData
      ),
      now()
    ),
    null
  )
)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;/strong&gt;you may need the regex if you can&amp;#39;t rely on consumer sending the proper data as per xml&amp;nbsp;standards&amp;nbsp; defined and agreed before hands. Might have to tweak the regex as well.&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62406?ContentTypeID=1</link><pubDate>Thu, 08 Nov 2018 07:57:31 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:b5feb69d-f0a7-46d8-a0c9-15f0ddb8d434</guid><dc:creator>jonathanb0001</dc:creator><description>What if I wanted to pass a CDT into JSON?&lt;br /&gt;
Would you recommend I create a parser rule to break it into a dictionary that JSON can translate properly?&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62386?ContentTypeID=1</link><pubDate>Wed, 07 Nov 2018 18:23:59 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:157e34a6-4f40-48c8-946c-75f4f3ad33f2</guid><dc:creator>Chris</dc:creator><description>For reference, when passing date values through JSON we utilize datetext() for input and todate() for output.&lt;br /&gt;
&lt;br /&gt;
Input: &lt;br /&gt;
&lt;br /&gt;
a!toJson(&lt;br /&gt;
 {&lt;br /&gt;
    dateFrom: if(rule!APN_isEmpty(local!dateFrom),&amp;quot;&amp;quot;,datetext(local!dateFrom,&amp;quot;MM/dd/yyyy&amp;quot;)),&lt;br /&gt;
    dateTo: if(rule!APN_isEmpty(local!dateTo),&amp;quot;&amp;quot;,datetext(local!dateTo,&amp;quot;MM/dd/yyyy&amp;quot;))&lt;br /&gt;
 }&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
local!search: a!fromJson(ri!input),&lt;br /&gt;
todate(local!search.dateFrom)&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62383?ContentTypeID=1</link><pubDate>Wed, 07 Nov 2018 15:28:58 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:ca7b2619-74bf-400e-bffc-4b6a759d98cd</guid><dc:creator>josep</dc:creator><description>Well your concern is good, let me see if I can comment anything else on the date as string. &lt;br /&gt;
&lt;br /&gt;
For your new requirement for datetime you can use it as decimal, from which the decimal part is the time. If you cast it to integer you are truncating the decimals consequently the time. &lt;br /&gt;
&lt;br /&gt;
Regarding your concern of what happens when you move between environments or different installations. Well the decimal/integer representation uses the date 1/1/2035 00:00 GMT (Appian epoch) as the value 0. That’s why the values are negative when you cast today to integer/decimal. So you can move the values freely between Appian installation. &lt;br /&gt;
&lt;br /&gt;
Jose&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62367?ContentTypeID=1</link><pubDate>Wed, 07 Nov 2018 08:13:43 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:3aef618a-86dc-4f29-afc3-eade86febf7b</guid><dc:creator>jonathanb0001</dc:creator><description>&lt;p&gt;Perhaps, but from what I can find the recommended way to pass dates to JSON is through the ISO 8601 standard.&amp;nbsp;&lt;a href="https://www.iso.org/iso-8601-date-and-time-format.html"&gt;www.iso.org/iso-8601-date-and-time-format.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Appian does seem to agree somewhat with this given thats how it casts dates into a JSON format, so why does it expect them to be passed in differently.&lt;/p&gt;
&lt;p&gt;Additionally what if this data isn&amp;#39;t originating from the same Appian location but from an external source,&amp;nbsp;I am not familiar with how Appian encodes dates to integers, is this an industry standard, so that&amp;nbsp;I can ensure that the data is converted to integer in a way that Appian can reverse correctly.&lt;/p&gt;
&lt;p&gt;Also this fails for datetimes, so it is kind of a hack.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!textField(
    label: todatetime(tointeger(now())),
    value: todatetime(tointeger(today()))
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I think that when casting to integer Appian rounds the datetime value up to the closest date value and then converts that to integer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62365?ContentTypeID=1</link><pubDate>Wed, 07 Nov 2018 03:04:22 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:5d758ee6-d4db-454c-8923-b9409434b68b</guid><dc:creator>josep</dc:creator><description>Hello, &lt;br /&gt;
&lt;br /&gt;
I have to say that passing the dates as integer is easier than handling it as strings. &lt;br /&gt;
&lt;br /&gt;
tointeger(today())&lt;br /&gt;
The drawback is that you have to cast it back to date( but that is something you are already doing as string in the code you shared) &lt;br /&gt;
&lt;br /&gt;
Hope this helps &lt;br /&gt;
&lt;br /&gt;
Jose&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62351?ContentTypeID=1</link><pubDate>Tue, 06 Nov 2018 15:53:31 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:3b2c786d-87dc-46ff-820c-a9858398400f</guid><dc:creator>harshav</dc:creator><description>Hi ,&lt;br /&gt;
&lt;br /&gt;
We do actually have the similar request of read and write the date format into the Appian DB, we do generally get the value as Text and convert them it todate . Ideally there is not straight way to do this AFAIK but there was some plugin available in App Market regarding Json Date.&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62339?ContentTypeID=1</link><pubDate>Tue, 06 Nov 2018 08:06:53 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:f6351b91-24f7-4170-899f-871f16fff4f0</guid><dc:creator>jonathanb0001</dc:creator><description>&lt;p&gt;Huh interestingly it even accepts the format of mmm/dd/yyyy. &lt;br /&gt; But if given mm/dd/yyyy it will interpret it incorrectly, except when the day is greater than 12 as it seems to do some logical shuffling when it sees an invalid month.&lt;br /&gt; Pretty strange and it would be great if we could get some explanation on how Appian expects to be given its Json Data, as I think I would prefer it to fail consistently than behave unexpectedly.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Casting dates to and from JSON</title><link>https://community.appian.com/thread/62319?ContentTypeID=1</link><pubDate>Mon, 05 Nov 2018 19:00:59 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:f170e796-d279-42ae-97c2-cce34f01f319</guid><dc:creator>Mike Schmitt</dc:creator><description>I&amp;#39;ve just double checked and it looks like it works pretty easily if you cast the date / datetime to string before doing the initial JSON conversion, and the resulting value coming back in from json can be cast back to date / datetime using &amp;quot;todatetime()&amp;quot;, though perhaps some of the inherent extra data (seconds, time zone, etc) is lost this way.&lt;br /&gt;
&lt;br /&gt;
I also am curious what the official recommended methodology is for retrieving the datetime value when the date data type itself is used in the JSON string.  It seems like there should be some way to do it automatically without needing to, say, build a manual parser helper rule.&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>