Working with DateTime in C/AL

Hi,

I need to subtract 2 hours from the current date and time, and then convert to ISO8601. I am able to get the new DateTime, but cannot find anything on the conversion? Would there be a simpler way to do this?

NewDate := CreateDateTime(TODAY,TIME);
NewDate - (2 * 60 * 60 * 1000)

T.I.A.

Answers

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    NewDate := CURRENTDATETIME - (2 * 60 * 60 * 1000)
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • EBothaEBotha Member Posts: 6
    edited 2018-07-02
    Thank you. I am getting the correct date and time, but any idea also to get it in ISO8601 format, eg.g. "2018-07-02T09%3A25%3A05Z"?
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    NewDateTxt := FORMAT(NewDate, 0, 9) - close enough?
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • EBothaEBotha Member Posts: 6
    @Slawek_Guzek I tweaked it a little to remove the milliseconds from the string, but it worked. Looks like the below:

    NewDateTxt := FORMAT(CURRENTDATETIME, 0, 9);
    NewDateTxt := DELSTR(NewDateTxt,STRPOS(NewDateTxt,'.'), 4);
    NewDateTxt := replaceString(FORMAT(NewDateTxt), ':', '%3A');

    Result = 2018-07-02T12%3A40%3A05Z

    Not sure if this is the best way but works for me. Thank you very much for your help
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-07-02
    I'd suggest you to read about formating the FORMAT output, and use custom formatting string - rather than manipulating the text output of FORMAT function to get rid of miliseconds. Your current code is going to produce incorrect results if the milisecond part will be shorten than 4 characters.

    Alternatively you can use the ROUNDDATETIME function to round the CURRENTDATETIME output to full sencods
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • EBothaEBotha Member Posts: 6
    edited 2018-07-05
    @Slawek_Guzek I took your advice and modified the code:

    Date1 := CREATEDATETIME(TODAY,TIME - (3600000*2));

    TimeStamp := FORMAT(Date1,0,'<Year4>-<Month,2>-<Day,2>T<Hours24>%3A<Minutes,2>%3A<Seconds,2>Z');

    Result : 2018-07-05T10%3A32%3A32%3A45Z

    Thank you for helping a newbie out. Much appreciated.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    everyone here was a newbie once :)
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
Sign In or Register to comment.