What is the easiest way to get current date and time and format it according to required format string, i.e. ‚DD.MM.YYYY hh.mm.ss‘ without using additional libraries like PLCnextBase?
I use RTC variable and convert its components to string, then concat them together: I know that it’s possible to use format specifier for time in TO_STRING function, but how to get current date and time in LDATE_AND_TIME data type? Function RTC_S doesn’t fit, because it returns STRING, not LDATE_AND_TIME.
This may not be the suggestion you’re looking for, but if you’re willing to create a C# library, there are nice tools in C# for getting and formatting DateTime.
Here’s a sample function block for getting DateTime:
[FunctionBlock]
public class DateTime_Now
{
[Output, DataType(„LDATE_AND_TIME“)]
public long DateTime;
[Initialization]
public void __Init()
{
}
[Execution]
public void __Process()
{
//Get current, local date time as timespan to output tick value as LDATE_AND_TIME
TimeSpan dateTimeNow = System.DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0);
DateTime = (long)(dateTimeNow.TotalMilliseconds * 1000000.0);
}
}
Here’s a sample function for getting DateTime as a formatted string:
[Function]
public static class DateTime_NowString
{
[Execution]
public static void __Process(
[Output] ref IecStringEx DateTime_NowString,
[Input] ref IecStringEx Format)
{
//Return the current datetime in the format specified
DateTime_NowString.Init(DateTime.Now.ToString(Format.ToString()));
}
}
Hello,
I don’t know exactly what you want to achieve yet. But if you work with the structure of type ‚ReSyBasic_SR_Clock‘, then you can put individual elements of date and time into or out of the structure as STRING or INT, or much more.
Example with the variable ‚Date_TIME‘:
- Date_TIME.SR_ClockInfo.I_Hour for hour
- Date_TIME.SR_ClockInfo.S_DateTime for date and time
- etc
Hello, </p>I don’t know exactly what you want to achieve yet. But if you work with the structure of type ‚ReSyBasic_SR_Clock‘, then you can put individual elements of date and time into or out of the structure as STRING or INT, or much more.</p>Example with the variable ‚Date_TIME‘:</p>- Date_TIME.SR_ClockInfo.I_Hour for hour</p>- Date_TIME.SR_ClockInfo.S_DateTime for date and time</p>- etc</p>[https://forum.plcnext-community.net/uploads/0BTRRXXQORZD/image.png</a></p>
I need to get current date and time and obtain formatted string according to the format I specify.
Hi!</p>[https://forum.plcnext-community.net/uploads/OOSQ0MF51CJK/d0-b8-d0-b7-d0-be-d0-b1-d1-80-d0-b0-d0-b6-d0-b5-d0-bd-d0-b8-d0-b5.png</a></p>Try this one embeded fc.</p>
Thank you, although it would be a little bit strange to concat data from RTC into LDT, then convert it into string…
So it looks like PLCnext doesn’t have a standard function to obtain current date and time in LDT…
Yes, I also format the INT values with CONTACT_LDT, going backwards with SPLIT_LDT.
In the LDT you can easily add time zones and summer/winter time. From the LDT you can use TO_STRING to output the desired format (STRING#‚{0:dd.MM.yyyy - hh:mm:ss}‘ or STRING#‚{0:hh:mm:ss}‘ or STRING#‚{0 :dd.MM.yyyy}‘)