Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to get system date...
Finance forum
Suggested answer

How to get system date time in stream

Posted on by 136
Hi Team,
 
Using below code i am generating the .txt file.
System.IO.Stream        stream;
TextStreamIo            io;
System.IO.StreamReader  reader;
utcDateTime             dateTime;
Str                     fileContent;
 
 dateTime = DateTimeUtil::getSystemDateTime();
     
 io = TextStreamIo::constructForWrite();
 io.writeExp([dateTime]);
 
 stream = io.getStream();
 stream.Position = 0;
 reader = new System.IO.StreamReader(stream);
 filecontent = reader.ReadToEnd();
 File::SendStringAsFileToUser(fileContent, 'MyTextFile.txt');
 
 info(strfmt("%1", dateTime));
In .txt file date time is coming like 2024-09-29T12:37:16Z37001
In info i can see 2024/09/29 13:08:37
 
Please let me know how to get system date time here.
  • Waed Ayyad Profile Picture
    Waed Ayyad 5,815 Super User 2024 Season 2 on at
    How to get system date time in stream
    Hi,

    Is your issue resolved?  If yes, mark the answers that helped you as verified.

    Thanks
    Waed Ayyad
  • RT-23081044-0 Profile Picture
    RT-23081044-0 4 on at
    How to get system date time in stream
    To get the system date and time in the desired format, convert the UTC date and time to local time before writing it to the file
     
    System.IO.Stream        stream;
    TextStreamIo            io;
    System.IO.StreamReader  reader;
    utcDateTime             utcDateTime;
    dateTime                localDateTime;
    Str                     fileContent;
    utcDateTime = DateTimeUtil::getSystemDateTime();
    localDateTime = DateTimeUtil::applyTimeZoneOffset(utcDateTime, DateTimeUtil::getUserPreferredTimeZone());
    io = TextStreamIo::constructForWrite();
    io.writeExp([localDateTime]);
    stream = io.getStream();
    stream.Position = 0;
    reader = new System.IO.StreamReader(stream);
    fileContent = reader.ReadToEnd();
    File::SendStringAsFileToUser(fileContent, 'MyTextFile.txt');
    info(strfmt("%1", localDateTime));


    thanks,
    ramjee
     
  • Suggested answer
    Bill Ngo Profile Picture
    Bill Ngo 337 on at
    How to get system date time in stream
     Hi CU,
     
    You should use format datetime2Str() for your case then you will get system date time in both Info() and csv file. 
     
            System.IO.Stream        stream;
            TextStreamIo            io;
            System.IO.StreamReader  reader;
            //utcDateTime             dateTime;
            Str                     fileContent;
            Str dateTime = datetime2Str(DateTimeUtil::getSystemDateTime(),DateFlags::FormatAll); //DateTimeUtil::getSystemDateTime();
       
            io = TextStreamIo::constructForWrite();
            io.writeExp([dateTime]);
     
            stream = io.getStream();
            stream.Position = 0;
            reader = new System.IO.StreamReader(stream);
            filecontent = reader.ReadToEnd();
            File::SendStringAsFileToUser(fileContent, 'MyTextFile.txt');
     
            info(strfmt("Date time is: %1", dateTime));
     
     
     
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 229,021 Most Valuable Professional on at
    How to get system date time in stream
    If you want a different format of the date and time, convert it to a string by yourself. Obviously, a text file contains text and therefore the date time value must be converted to text. Your current code doesn't specify any format, therefore the default one is used.
     
    You can use either X++ methods, such as DateTimeUtil::toFormattedStr(), or .NET ones, such as System.String::Format().
     
    utcNow() isn't a solution, because it still returns a date and time value, not a string.
  • Suggested answer
    Waed Ayyad Profile Picture
    Waed Ayyad 5,815 Super User 2024 Season 2 on at
    How to get system date time in stream
    Hi,
     
    You can use utcNow() method to get currentDateTime in UTC.
     
    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Forum Structure Changes Coming Soon!

Quick Links

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,734 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,021 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,150

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans