datepickerc++20winui-3c++-chrono

Convert time_point -> Rep and back again (Rep -> time_point)


How to update date in a datepicker element. I have a int64_t attribute m_myDate that contains the date.

MainWindow.xaml.cpp

    ...
    void MainWindow::saveBtn_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args)
    {
        DateTime dateTime;
        dateTime = myDatePicker().Date();
        m_myDate = dateTime.time_since_epoch().count();
    }

    void MainWindow::loadBtn_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args)
    {
        DateTime dateTime;
        dateTime.time_since_epoch(std::chrono::nanoseconds(m_myDate)); // replace this with correct code
        myDatePicker().Date(dateTime);
    }
    ...

Environment c++20 winui3

All files as a VS 2022 project https://1drv.ms/u/s!AsM8s08DJk30u5kAsOK-A6MOZ8YiSg?e=whhfzN


Solution

  •     void MainWindow::saveBtn_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args)
        {
            DateTime dateTime;
            dateTime = myDatePicker().Date();
            m_myDate = dateTime.time_since_epoch().count();
            myTextBox().Text(to_hstring(m_myDate));
        }
    
        void MainWindow::loadBtn_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args)
        {
            myDatePicker().SelectedDate(winrt::Windows::Foundation::DateTime{ winrt::Windows::Foundation::TimeSpan{m_myDate} });
            myDatePicker().UpdateLayout();
        }
    

    See https://github.com/TirsvadGUI/Cpp.Winui3.Example-Controls/tree/master/src/DatePicker