locked
Why can't I find Windows::Foundation::DateTime::Now?

    Question

  • From looking at this page it appears I can get the current date by reading from Windows::Foundation::DateTime::Now. But when I attempt to call it I'm getting an error that it doesn't exists.  Am I don't something wrong or is this member not implemented?
    Joel Ivory Johnson | http://www.j2i.net | Windows Phone Developer MVP
    It takes all the running you can do to stay in one place.If you want to get somewhere else,you must try to run at least twice as fast as that.
    Saturday, October 08, 2011 7:27 PM

Answers

All replies

  • That page does not define Windows::Foundation::DateTime::Now,

     http://msdn.microsoft.com/en-us/library/windows/apps/windows.foundation.datetime(v=VS.85).aspx  does.

     

    Try:

    void hello::MainPage::HelloButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
      	Windows::Globalization::Calendar^ c = ref new Windows::Globalization::Calendar;
    	c->Now();
    	DisplayText->Text= c->DayOfWeekAsString();
    
    }
    

     

    Sunday, October 09, 2011 4:15 PM
  • Thanks, I am able to get the date with the code you shared. I have a question from this.

    In the page that I referenced there is a C++ code example for a Metro style program (under the heading "Implemented Interfaces") that demonstrates how to get the DateTime using code that I have not been able to successfully compile. Here is a small section from that example code.

     

     

    int main()
    {
       int window = 10;
       int freq = 60 * 60 * 2; // 2 hours;
    
       DateTime d1 = DateTime::Now;
       DateTime d2 = d1.AddSeconds( 2 * window );
    }
    

    I now get the feeling that this code may be incorrect and some one writing the documentation had just converted a C# example to C++ syntax without realizing that this method isn't available in C++. Do you know if it is possible to call a ::Now method on the DateTime type from C++? If not then I need to leave feedback for this page to be updated.

     

     


    Joel Ivory Johnson | http://www.j2i.net | Windows Phone Developer MVP
    It takes all the running you can do to stay in one place.If you want to get somewhere else,you must try to run at least twice as fast as that.

    Sunday, October 09, 2011 5:18 PM
  • From the version section, System::DateTime is a .NET Framework class (supporting J# too).  So not surprising it won't compile.

    Windows::Foundation::DateTime is a Windows Runtime structure with one field, UniversalTime. 

    I note there is a Calendar-> http://msdn.microsoft.com/en-us/library/windows/apps/windows.globalization.calendar.todatetime(v=VS.85).aspx toDateTime methods which returns a DateTime.

    Yes, it looks like the documentation for Metro C++ is pre-beta.

     

    Sunday, October 09, 2011 5:50 PM
  • Thanks, I am able to get the date with the code you shared. I have a question from this.

    In the page that I referenced there is a C++ code example for a Metro style program (under the heading "Implemented Interfaces") that demonstrates how to get the DateTime using code that I have not been able to successfully compile. Here is a small section from that example code.

     

     

    int main()
    {
       int window = 10;
       int freq = 60 * 60 * 2; // 2 hours;
    
       DateTime d1 = DateTime::Now;
       DateTime d2 = d1.AddSeconds( 2 * window );
    }
    

    I now get the feeling that this code may be incorrect and some one writing the documentation had just converted a C# example to C++ syntax without realizing that this method isn't available in C++. Do you know if it is possible to call a ::Now method on the DateTime type from C++? If not then I need to leave feedback for this page to be updated.

     

     


    Joel Ivory Johnson | http://www.j2i.net | Windows Phone Developer MVP
    It takes all the running you can do to stay in one place.If you want to get somewhere else,you must try to run at least twice as fast as that.


    Joel,

    That is C++/CLI code (managed) which needs the /clr compilation switch (not compatible with /ZW needed for WinRT).


    http://blog.voidnish.com
    Monday, October 10, 2011 4:02 PM
  • Windows::Globalization::Calendar now uses SetToNow() instead. http://msdn.microsoft.com/en-us/library/windows/apps/windows.globalization.calendar.settonow.aspx
    Sunday, July 08, 2012 4:36 PM