web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to get system date time in stream

(1) ShareShare
ReportReport
Posted on by 230
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.
I have the same question (0)
  • Suggested answer
    Waed Ayyad Profile Picture
    9,039 Super User 2025 Season 2 on at
    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
     
  • Suggested answer
    Martin Dráb Profile Picture
    237,987 Most Valuable Professional on at
    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
    Bill Ngo Profile Picture
    372 on at
     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));
     
     
     
  • RT-23081044-0 Profile Picture
    6 on at
    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
     
  • Waed Ayyad Profile Picture
    9,039 Super User 2025 Season 2 on at
    Hi,

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

    Thanks
    Waed Ayyad

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

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 490 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans