Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

How to get system date time in stream

(1) ShareShare
ReportReport
Posted on by 161
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 6,360 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 370 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 230,370 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 6,360 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

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans