locked
Get ISO 8601 current time RRS feed

  • Question

  • How can I get std::string current date time in ISO 8601 ?
    Sunday, May 10, 2020 9:06 PM

Answers

  • Hi

    Thank you for posting here.

    >>How can I get std::string current date time in ISO 8601 ?

    I suggest you could try to use system_clock::now to return the current time. And then you could convert the timestamp to ISO8601 format with strftime function.

    Here is a demo:

    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
        time_t current_time = std::chrono::system_clock::to_time_t(now);
        char buf[32];
        strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&current_time));
        //also you can use
        //strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&current_time));
        std::string s_CurrentTime(buf);
        std::cout<<buf<<std::endl;

    Best Regards,

    Jeanine Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, May 11, 2020 5:26 AM

All replies

  • Look up time() and gmtime() in your reference.
    Monday, May 11, 2020 4:41 AM
  • Hi

    Thank you for posting here.

    >>How can I get std::string current date time in ISO 8601 ?

    I suggest you could try to use system_clock::now to return the current time. And then you could convert the timestamp to ISO8601 format with strftime function.

    Here is a demo:

    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
        time_t current_time = std::chrono::system_clock::to_time_t(now);
        char buf[32];
        strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&current_time));
        //also you can use
        //strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&current_time));
        std::string s_CurrentTime(buf);
        std::cout<<buf<<std::endl;

    Best Regards,

    Jeanine Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, May 11, 2020 5:26 AM
  • Hi,

    Have you got any updates? If your case has been solved, please help to mark answers. If not, just feel free to contact us.

    Your understanding and cooperation will be grateful.

    Best Regards,

    Jeanine Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 20, 2020 5:29 AM