Set simulation's date and time in correct format

Set simulation's date and time in correct format

Stain23
Not applicable
25 Views
3 Replies
Message 1 of 4

Set simulation's date and time in correct format

Stain23
Not applicable

[ FlexSim 24.2.0 ]

I'm trying to make this api request work. In my simulation I have this code which makes an api request to get the exact date and time in the following format 2024-08-30 08:01:00, but when I try to set it, it remains the current one. How can I fix this problem?

Thanks to whoever will reply at this post.


PS: for security reasons, I intentionallly omitted the request link and correspondant key.


/**Set Date and Time*/
Object current = ownerobject(c);

Http.Request request = Http.Request("request");
request.method = Http.Method.Get;
request.headers = "key";


Http.Response response = request.sendAndWait();
string value = response.value;


Map result = JSON.parse(value);
Array data = result["data"];
Map firstElement = data[0];  
string last_updated = firstElement["last_updated", 1]; 


DateTime dt=DateTime(last_updated);


print(dt);  
function_s(getmodelunit(START_TIME_NODE), "setDateTime", dt);


0 Likes
Accepted solutions (1)
26 Views
3 Replies
Replies (3)
Message 2 of 4

moehlmann_fe
Explorer
Explorer
Accepted solution

In line 15 it should be "data[1]" (in FlexSim only bundles are 0-indexed). In line 16 the map key should be a single value. If the key is in array, pass it in as an array (firstElement[["last_updated", 1]]).

In line 19 you don't specify the format. This means that FlexSim will assume the date time is formatted according to the Windows default which will depend on your OS' language setting.

0 Likes
Message 3 of 4

fapicella
Enthusiast
Enthusiast

Hi,

In general when you have a Datetime variable and you want to save it in a string with a specific format, you can use the "toString" Datetime method and specify the format you want. In your case you could use the line 3 of code in the picture. I advice you to consult the manual in the Datetime section, there you can fine all the characters you can insert to build your format. https://docs.flexsim.com/en/24.0/Reference/CodingInFlexSim/FlexScriptAPIReference/Data/DateTime.html...

Instead, if you need to go from a string to Datetime format you have to use line 8 of code in the picture, also using the same construction methodology as the format you are converting.

I hope I have been of help to you!


picture.png


0 Likes
Message 4 of 4

logan_gold
Community Manager
Community Manager

Hi @Stain, was Felix Möhlmann's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes