xtd 0.2.0
Loading...
Searching...
No Matches

◆ now()

static date_time xtd::date_time::now ( )
staticnoexcept

Gets a xtd::date_time object that is set to the current date and time on this computer, expressed as the local time.

Returns
An object whose value is the current local date and time.
Examples
The following example uses the xtd::date_time::now and xtd::date_time::utc_now properties to retrieve the current local date and time and the current universal coordinated (UTC) date and time. It then uses the formatting conventions of a number of locale to display the strings, along with the values of the their xtd::datte_time::kind properties.
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::collections::generic;
class program {
public:
static auto main() {
auto local_date = date_time::now();
auto utc_date = date_time::utc_now();
auto locale_names = list {"en_US", "en_GB", "fr_FR", "de_DE", "ru_RU"};
for (auto locale_name : locale_names) {
try {
std::locale::global(std::locale {locale_name + ".utf-8"_s});
console::write_line("{}:", locale_name);
console::write_line(" Local date and time: {}, {}", local_date.to_string(), local_date.kind());
console::write_line(" UTC date and time: {}, {}\n", utc_date.to_string(), utc_date.kind());
} catch (const std::exception& e) {
console::write_line(string::format("Make sure {} locale is installed on your system :\n\n{}\n", locale_name, e.what()), "Exception");
}
}
}
};
startup_(program::main);
// This code can produce the following output :
//
// en_US
// Local date and time: Sun Jan 2 09:50:36 2022, local
// UTC date and time: Sun Jan 2 08:50:36 2022, utc
//
// en_GB
// Local date and time: Sun 2 Jan 09:50:36 2022, local
// UTC date and time: Sun 2 Jan 08:50:36 2022, utc
//
// fr_FR
// Local date and time: Dim 2 jan 09:50:36 2022, local
// UTC date and time: Dim 2 jan 08:50:36 2022, utc
//
// de_DE
// Local date and time: So 2 Jan 09:50:36 2022, local
// UTC date and time: So 2 Jan 08:50:36 2022, utc
//
// ru_RU
// Local date and time: воскресенье, 2 января 2022 г. 09:50:36, local
// UTC date and time: воскресенье, 2 января 2022 г. 08:50:36, utc
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
@ e
The E key.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
The xtd::date_time::now property returns a xtd::date_time value that represents the current date and time on the local computer. Note that there is a difference between a xtd::date_time value, which represents the number of ticks that have elapsed since midnight of January 1, 0001, and the string representation of that xtd::date_time value, which expresses a date and time value in a culture-specific-specific format. For information on formatting date and time values, see the to_string method. The following example displays the short date and time string in a number of culture-specific formats.
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::collections::generic;
class program {
public:
static auto main() {
auto local_date = date_time::now();
auto locale_names = list {"en_US", "en_GB", "fr_FR", "de_DE", "ru_RU"};
for (auto locale_name : locale_names) {
try {
std::locale::global(std::locale {locale_name + ".utf-8"_s});
console::write_line("{}: {}", locale_name, date_time::sprintf("%x %T", local_date));
} catch (const std::exception& e) {
console::write_line(string::format("Make sure {} locale is installed on your system :\n\n{}\n", locale_name, e.what()), "Exception");
}
}
}
};
startup_(program::main);
// This code can produce the following output :
//
// en_US: 01/02/2022 10:59:07
// en_GB: 02/01/2022 10:59:07
// fr_FR: 02.01.2022 10:59:07
// de_DE: 02.01.2022 10:59:07
// ru_RU: 02.01.2022 10:59:07
Examples
date_time_add.cpp, date_time_add_days.cpp, and sprintf_date_time.cpp.