Convert DateTime to XML

kjboloekjboloe Member Posts: 56
Hi, I have a problem in a NAV 2009.

I would like to get the correct datetime format for a XML.

I have tried to use :

message(format(DT2DATE(CURRENTDATETIME),0,9) + 'T' + format(DT2TIME(CURRENTDATETIME),0,9));

The result is correct for the date part, but the timepart isn't.
the time is now 12:04.

Microsoft Dynamics NAV Classic
2012-05-02T10:04:28.611Z
OK

I have seen other subjects concerning this issue.
Kind regards

Kenneth Jarlshøi Bolø
Dynateam A/S

Comments

  • SogSog Member Posts: 1,023
    Format(currentdatetime,0,9) gives me the following:
    It's currently 13:45
    Microsoft Dynamics NAV Classic
    2012-05-02T11:45:26.507Z

    The 2 hours less should be because it's GMT+2 (daylight savings is in effect)
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • KYDutchieKYDutchie Member Posts: 345
    Kenneth,

    this is caused by your second parameter in the Format function.
    You have
    format(DT2TIME(CURRENTDATETIME),0,9)
    
    The 9 indicates you switch to XML date.
    The Z indicates that the time is expressed in 'Zulu' time which is GMT.
    If you use either 0 or 1 instead of the 9 you should be fine.

    Hope this helps,

    Regards,

    Willy
    Fostering a homeless, abused child is the hardest yet most rewarding thing I have ever done.
  • kjboloekjboloe Member Posts: 56
    Hi Willy
    I have tried it, but it doesn't give me the correct datetime format for XML

    http://www.w3.org/TR/NOTE-datetime

    I have made a solution so that I get a converted string : DD:MM:YYYYTHH:MM:SS

    Thanks for all your help.
    Kind regards

    Kenneth Jarlshøi Bolø
    Dynateam A/S
  • ta5ta5 Member Posts: 1,164
    edited 2012-05-03
    Hi Kenneth
    Is it possible to share your solution? And last but not least: What were the requirements and why was the xml format with "zulu" notation not suitable?

    Thanks
    Thomas
  • kjboloekjboloe Member Posts: 56
    Hi Thomas

    The Zulu time isn't correct in Denmark, because it is 2 hours behind.

    Date : 3/5-2012, time : 08:55:31 = 2012-05-03T08.55.31

    We have ignored the timezone for the moment, because the company I integrate with is also placed in Denmark \:D/

    This is my solution :

    OBJECT Codeunit 90000 test
    {
    OBJECT-PROPERTIES
    {
    Date=03-05-12;
    Time=08:52:56;
    Version List=BDL1.24;
    }
    PROPERTIES
    {
    OnRun=VAR
    DateVar@1000000000 : Integer;
    BEGIN
    MESSAGE(DateTimeToText(CURRENTDATETIME));
    END;

    }
    CODE
    {
    VAR
    TimeVar@1000000000 : Time;

    LOCAL PROCEDURE DateToText@1000000002(VarDate@1000 : Date) : Text[20];
    BEGIN
    IF VarDate = 0D THEN
    EXIT('');
    EXIT(FORMAT(VarDate,0,'<Year4>-<Month,2>-<Day,2>'));
    END;

    PROCEDURE DateTimeToText@1000000010(VarDateTime@1000000000 : DateTime) : Text[30];
    BEGIN
    VarDateTime := ROUNDDATETIME(VarDateTime);
    IF DT2DATE(VarDateTime) = 0D THEN
    EXIT('');
    EXIT(DateToText(DT2DATE(VarDateTime)) + 'T' + TimeToText(DT2TIME(VarDateTime)));
    END;

    LOCAL PROCEDURE TimeToText@1000000008(VarTime@1000 : Time) : Text[20];
    VAR
    Hours@1000000000 : Text[2];
    Minuts@1000000001 : Text[2];
    Seconds@1000000002 : Text[2];
    BEGIN
    IF VarTime = 0T THEN
    EXIT('00.00.00');

    Hours := FORMAT(VarTime,0,'<Hours24,2>');
    IF COPYSTR(Hours,1,1) = ' ' THEN
    Hours := '0' + COPYSTR(Hours,2,1);
    Minuts := FORMAT(VarTime,0,'<Minutes,2>');
    Seconds := FORMAT(VarTime,0,'<Seconds,2>');
    EXIT(Hours + '.' + Minuts + '.' + Seconds);
    END;

    BEGIN
    END.
    }
    }
    Kind regards

    Kenneth Jarlshøi Bolø
    Dynateam A/S
  • ta5ta5 Member Posts: 1,164
    Hi Kenneth
    Thanks, thats why I asked. Imo you can use Zulu-Time, but in this case you have to consider the offset if you need local time. And the offset is not always the same because of daylight saving period in summer months. Whether to use Zulu-Time or local time of course depends on the use case of the application.

    Regards
    Thomas
  • mdPartnerNLmdPartnerNL Member Posts: 802
    KYDutchie wrote:
    Kenneth,

    this is caused by your second parameter in the Format function.
    You have
    format(DT2TIME(CURRENTDATETIME),0,9)
    
    The 9 indicates you switch to XML date.
    The Z indicates that the time is expressed in 'Zulu' time which is GMT.
    If you use either 0 or 1 instead of the 9 you should be fine.

    Hope this helps,

    Regards,

    Willy

    This will work only if you are in the US, in other countries 0 or 1 will use the regional settings of that country.
Sign In or Register to comment.