Store formated date into variable

Post your feature requets for new triggers, conditions, actions and other improvements.

Moderator: Martin

xxx
Posts: 39
Joined: 18 Mar 2013 18:51

Store formated date into variable

Post by xxx » 18 Mar 2013 20:37

Hi, I want to convert a date into a readable format using "getDate(),dateformat,dd.MM.yyyy HH:mm:ss" and store this string into a new variable.
This does not work directly at the moment. Correct me if I am wrong.
Currently I a have a work arround storing the date in the readable format into a text file and read it back into a variable. It works but it would be great if there will be the possibility to do it directly.

Thanks.

inReinbek
Posts: 95
Joined: 02 Feb 2013 22:04

Re: Store formated date into variable

Post by inReinbek » 19 Mar 2013 13:16

Set it up as a global variable.

xxx
Posts: 39
Joined: 18 Mar 2013 18:51

Re: Store formated date into variable

Post by xxx » 19 Mar 2013 17:33

Hi, I want to do it that way:

Script: global_test={getDate(),dateformat,dd.MM.yyyy HH:mm:ss};

but it does not work.
global_test is declared as a string.

inReinbek
Posts: 95
Joined: 02 Feb 2013 22:04

Re: Store formated date into variable

Post by inReinbek » 19 Mar 2013 19:40

Try this:
Change the declaration to "Date" ... I've tried it ... it works well.
Does it also work at your side?

xxx
Posts: 39
Joined: 18 Mar 2013 18:51

Re: Store formated date into variable

Post by xxx » 20 Mar 2013 18:11

Unfortunately not. I changed the data type to date but it still not work. There are red colored errors shown in the action 'script'. And if I run the flow it stops there due to an error.

I also tried this:

Script: eval("global_test=getDate(),dateformat,dd.MM.yyyy HH:mm:ss");

But it does not work.

bobchernow
Posts: 56
Joined: 16 Mar 2013 14:10

Re: Store formated date into variable

Post by bobchernow » 20 Mar 2013 21:31

I have been trying to follow this with little luck. I think when you format the date it becomes a string. Without the formatting it looks like a long integer.

Please upload the flow so l can look at it and the error.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Store formated date into variable

Post by Martin » 21 Mar 2013 10:43

Please try the following (note the doublequotes):
global_test="{getDate(),dateformat,dd.MM.yyyy HH:mm:ss}"

xxx
Posts: 39
Joined: 18 Mar 2013 18:51

Re: Store formated date into variable

Post by xxx » 21 Mar 2013 18:55

Hi Martin,

thank you. It works now. :-)

Tender
Posts: 11
Joined: 03 Apr 2013 05:10

Re: Store formated date into variable

Post by Tender » 06 Apr 2013 18:01

Hi,

i have tried this, and it works:
datum="{getDate(),dateformat,dd.MM.yyyy HH:mm:ss}"

But what i need is a function that calculate a given UTC time into it's MESZ time.
The given UTC time is in a human readable format, like: 20130425-192000 (25.April 2013 - 19:20:00 o'clock)
I think, it is possible to add 2 hours to utc with

UTC_date=getDate(2013,04,25,19,20,00) // UTC_date is now "1366910400000"
MESZ_date=addHours({UTC_date}, 2) // MESZ_date is now "1366917600000"

Now i want to turn the java date into a human readable format, like this: 20130425-212000 - But no one of my codes worked:

Human_MESZ=getDate({MESZ_date},dd.MM.yyyy HH:mm:ss) // Could not execute...
Human_MESZ=getDate("{MESZ_date}",dd.MM.yyyy HH:mm:ss) // Could not execute...
Human_MESZ=getDate(MESZ_date,dd.MM.yyyy HH:mm:ss) // Could not execute...
Human_MESZ="getDate({MESZ_date},dd.MM.yyyy HH:mm:ss)" // Human_MESZ is then "getDate(1366917600000,dd.MM.yyyy HH:mm:ss)"
Human_MESZ={getDate({MESZ_date},dd.MM.yyyy HH:mm:ss)} // Could not execute...
Human_MESZ="{getDate({MESZ_date},dd.MM.yyyy HH:mm:ss)}" // Human_MESZ is "{error}"
Human_MESZ={getDate({MESZ_date}),dateformat,dd.MM.yyyy HH:mm:ss} // Could not execute...
Human_MESZ="{getDate({MESZ_date}),dateformat,dd.MM.yyyy HH:mm:ss}" // Human_MESZ is "{error}"
Human_MESZ="{getDate({MESZ_date}),dd.MM.yyyy HH:mm:ss}" // Could not execute...
Human_MESZ="{getDate(MESZ_date),dd.MM.yyyy HH:mm:ss}" // Could not execute...
Human_MESZ="{getDate(),{MESZ_date},dd.MM.yyyy HH:mm:ss}" // Could not execute...
Human_MESZ={getDate({MESZ_date}),dd.MM.yyyy HH:mm:ss} // Could not execute...
Human_MESZ="{getDate({MESZ_date}),dd.MM.yyyy HH:mm:ss}" // Human_MESZ is "{error}"

Even when i put the java date directly into the code, i'll get only errors:

Human_MESZ=getDate(1366917600000,dd.MM.yyyy HH:mm:ss) // Could not execute...
Human_MESZ="{getDate(1366917600000),dateformat,dd.MM.yyyy HH:mm:ss}" // Human_MESZ is "{error}"
Human_MESZ="{getDate("1366917600000"),dateformat,dd.MM.yyyy HH:mm:ss}" // Could not execute...

Where is my problem?? ;)

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Store formated date into variable

Post by Martin » 08 Apr 2013 07:59

Hi,

Automagic uses the milliseconds since 1970 for all dates (it does not wrap this value in a Date object like it is often done in Java). When you have a variable with the millis, then you are already done and can directly use the variable in a string inline expression to format the number.
Following should work in a scripting context (in an action Script, condition Expression etc.):

UTC_date=getDate(2013,04,25,19,20,00);
MESZ_date=addHours(UTC_date, 2);//no need for curly braces around UTC_date

Human_MESZ="{MESZ_date,dateformat,dd.MM.yyyy HH:mm:ss}";

Inline formatting syntax
{expression,formattype,pattern}

expression can be either a variable like MESZ_date, a function call like getDate(), a plain value like 1366910400000 or a small script.
formattype must be dateformat in this case
pattern the date-pattern to use to format the date (like dd.MM.yyyy HH:mm:ss)

Examples (in a script context):
x="{MESZ_date,dateformat,HH:mm}";
x="{1366910400000,dateformat,HH:mm}";
x="{getDate(),dateformat,HH:mm}";
x="{addHours(getDate(),2),dateformat,HH:mm}";

or since a date is just a number (millis since 1970):
x="{getDate()+2*60*60*1000,dateformat,HH:mm}";

The syntax is almost the same in regular text fields of many actions.
Examples (in a non-script context like the text field of an action Notification on Screen):
{MESZ_date,dateformat,HH:mm}
{1366910400000,dateformat,HH:mm}
{getDate(),dateformat,HH:mm}
{addHours(getDate(),2),dateformat,HH:mm}

Regards,
Martin

Locked