question

Stain avatar image
0 Likes"
Stain asked Logan Gold commented

Set simulation's date and time in correct format

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);


FlexSim 24.2.0
datetimetroubleshootingsetdate
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Fiorenzo A avatar image Fiorenzo A commented ·

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#Method-toString

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 0 ·
picture.png (42.0 KiB)
Logan Gold avatar image Logan Gold ♦♦ commented ·

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 0 ·

1 Answer

Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann answered

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.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.