【问题标题】:How to create an hstring from C++/WinRT DateTime?如何从 C++/WinRT DateTime 创建一个 hstring?
【发布时间】:2019-04-25 17:27:55
【问题描述】:

我想知道最简单的转换方法是什么 Windows::Foundation::DateTime dt = winrt::clock::now(); 转换成 hstring?

【问题讨论】:

  • 字符串表示应该包含哪些信息?

标签: c++-winrt


【解决方案1】:

本身没有“转换”。我怀疑您正在寻找的是某种日期/时间格式化实用程序。

C++20 将在该领域带来极大改进的支持,但目前还没有。

可移植但功能较差的方法是转换为 time_t 并使用 C 库。 https://en.cppreference.com/w/cpp/chrono/c

在 WinRT 中执行此操作的全功能方法是在此处使用日期格式化实用程序 https://docs.microsoft.com/en-us/uwp/api/windows.globalization.datetimeformatting

【讨论】:

    【解决方案2】:

    它不漂亮,但你可以这样做:

    winrt::Windows::Foundation::DateTime dt = winrt::clock::now();
    std::time_t t = dt.time_since_epoch().count();
    
    std::wstringstream wss;
    wss << std::put_time(std::localtime(&t), L"%m/%d/%Y %H:%M:%S");
    winrt::hstring hs{ wss.str() };
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2020-05-16
      • 2015-09-03
      相关资源
      最近更新 更多