简体   繁体   中英

Get the Local Time in C++ Visual Studio 2015 Community

I have looked all over for ways to get the current time in C++ for a Console Application project, but every method I have found has been rejected by Visual Studio as incorrect or deprecated, even with #define tags to keep the program from deprecating the functions. What is the current method to get the current time in a Visual Studio C++ Win32 console application?

You may use DateTime::Now

using namespace System;
using namespace System::Globalization;

void main()
{
   DateTime localDate = DateTime::Now;
   array<String^>^ cultureNames = { "en-US", "en-GB", "fr-FR",
                                    "de-DE", "ru-RU" };

   for each (String^ cultureName in cultureNames) {
      CultureInfo^ culture = gcnew CultureInfo(cultureName);
      Console::WriteLine("{0}: {1}", cultureName,
                         localDate.ToString(culture));
   }
}

For more information, have a look into MSDN

Feel free to use this free, open-source modern C++ library :

#include "tz.h"
#include <iostream>

int
main()
{
    std::cout << date::make_zoned(date::current_zone(),
                                  std::chrono::system_clock::now()) << '\n';
}

See the installation section for details on how to install this on Windows. It is known to work with VS-2015.

The library is thread safe and uses no deprecated functionality. It also does a lot more things than just getting the current local time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM