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

◆ to_string() [3/3]

xtd::string xtd::date_time::to_string ( const string format,
const std::locale &  loc 
) const
overridevirtual

Converts the value of the current xtd::date_time object to its equivalent string representation using the specified format and the formatting conventions of the current culture.

Parameters
formatA standard or custom date and time format string.
locAn std::locale object that contains locale information (see std::locale).
Returns
A string representation of value of the current xtd::date_time object as specified by format.
Exceptions
xtd::format_excpetionThe length of format is 1, and it is not a valid format characters
-or-
The length si greater than 1.
Examples
The foloowwing example shows how to use xtd::date_time::to_string(const xtd::string&) with differentt formats.
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
using namespace xtd;
class program {
public:
static auto main() {
auto date1 = date_time {2008, 4, 1, 18, 7, 5, 16};
console::write_line(date1.to_string("a")); // PM
console::write_line(date1.to_string("b")); // 016
console::write_line(date1.to_string("B")); // 16
console::write_line(date1.to_string("c")); // 0160000
console::write_line(date1.to_string("c")); // 160000
console::write_line(date1.to_string("d")); // 04/01/2008
console::write_line(date1.to_string("D")); // 4/01/2008
console::write_line(date1.to_string("e")); // 05
console::write_line(date1.to_string("E")); // 5
console::write_line(date1.to_string("f")); // Tue Apr 1 18:07:05 2008
console::write_line(date1.to_string("F")); // Tue Apr 1 18:07:05 2008
console::write_line(date1.to_string("g")); // Tue Apr 1 18:07:05 2008
console::write_line(date1.to_string("G")); // Tue Apr 1 18:07:05 2008
console::write_line(date1.to_string("h")); // Tue
console::write_line(date1.to_string("H")); // Tuesday
console::write_line(date1.to_string("i")); // 01
console::write_line(date1.to_string("I")); // 1
console::write_line(date1.to_string("j")); // Apr
console::write_line(date1.to_string("J")); // April
console::write_line(date1.to_string("k")); // 04
console::write_line(date1.to_string("K")); // 4
console::write_line(date1.to_string("l")); // 08
console::write_line(date1.to_string("L")); // 2008
console::write_line(date1.to_string("m")); // 2008
console::write_line(date1.to_string("M")); // April 1
console::write_line(date1.to_string("n")); // Tuesday, 1 April 2008
console::write_line(date1.to_string("N")); // Tuesday, 1 April 2008 18:07:05
console::write_line(date1.to_string("o")); // 1 April 2008
console::write_line(date1.to_string("O")); // 1 April 2008
console::write_line(date1.to_string("p")); // 07
console::write_line(date1.to_string("P")); // 7
console::write_line(date1.to_string("s")); // 2008-04-01T18:07:05.0160000
console::write_line(date1.to_string("t")); // 18:07:05
console::write_line(date1.to_string("T")); // 18:07:05
console::write_line(date1.to_string("u")); // 2008-04-01 18:07:05
console::write_line(date1.to_string("U")); // Tuesday, 1 April 2008 18:07:05
console::write_line(date1.to_string("v")); // 18:07
console::write_line(date1.to_string("V")); // 18:07
console::write_line(date1.to_string("w")); // 18
console::write_line(date1.to_string("W")); // 18
console::write_line(date1.to_string("x")); // 06
console::write_line(date1.to_string("X")); // 6
console::write_line(date1.to_string("y")); // April 8
console::write_line(date1.to_string("Y")); // April 2008
console::write_line(date1.to_string("z")); // UTC
console::write_line(date1.to_string("Z")); // UTC
// Other format generate a xtd::format_exception exception
}
};
startup_(program::main);
// This code produces the following output :
//
// PM
// 016
// 16
// 0160000
// 160000
// 04/01/2008
// 4/01/2008
// 05
// 5
// Tue Apr 1 18:07:05 2008
// Tue Apr 1 18:07:05 2008
// Tue Apr 1 18:07:05 2008
// Tue Apr 1 18:07:05 2008
// Tue
// Tuesday
// 01
// 1
// Apr
// April
// 04
// 4
// 08
// 2008
// 2008
// April 1
// Tuesday, 1 April 2008
// Tuesday, 1 April 2008 18:07:05
// 1 April 2008
// 1 April 2008
// 07
// 7
// 2008-04-01T18:07:05.0160000
// 18:07:05
// 18:07:05
// 2008-04-01 18:07:05
// Tuesday, 1 April 2008 18:07:05
// 18:07
// 18:07
// 18
// 18
// 06
// 6
// April 8
// April 2008
// UTC
// UTC
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.h:85
#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
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
The formatting codes for xtd::date_time::to_string (const xtd::string&) are listed below:
Format Print
'a' writes "PM" or "AM"
'b' writes 3 digit millisecond, e.G. 012
'B' wrties millisecond, eg 12
'c' writes 7 digit tick, e.G. 0000123
'C' writes tick, e.G. 123
'd' writes date MM/dd/y, e.g. 01/05/42
'D' writes date M/dd/y, e.g. 1/055/42
'e' writes 2 digit second, e.G. 03
'B' writes second, e.G. 3
'f' writes alternative date and time string, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale.
'F' writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent).
'g' writes alternative date and time string, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale.
'G' writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent).
'h' writes abbreviated weekday name, e.g. Fri (locale dependent).
'H' writes full weekday name, e.g. Friday (locale dependent).
'i' writes 2 digit day, e.g. 05
'I' writes day, e.G. 5
'j' writes abbreviated month name, e.g. Oct (locale dependent)
'J' writes full month name, e.g. October (locale dependent).
'k' writes 2 digit month, e.g. 01
'K' writes month, e.g. 1
'l' writes 2 digit year, e.g. 71
'L' writes 4 digit year, e.g. 1971
'm' writes year, e.g. 42
'M' writes full month name and day, e.g. October, 5 (locale dependent).
'n' writes full weekday name, day, fulll month and year, e.g. Tuesday, 1 April 2008
'N' writes full weekday name, day, fulll month, year, hour, minute ans second, e.g. Tuesday, 1 April 2008 18:07:05
'o' writes day, full month and year, e.g. 5 January 42
'O' writes day, full month and year, e.g. 5 January 42
'p' writes 2 digit minute, e.G. 06
'P' writes minute, e.G. 6
's' writes sortable date/time pattern yyyy-MM-ddThh:mm:ss.ttttttt. e.g. 1971-05-01T21:32:24:42.004567
't' writes time hh/mm/ss, e.g. 02:04:06
'T' writes time h/mm/ss, e.g. 2:04:06
'u' writes sortable short date/time pattern y-MM-dd hh:mm:ss. e.g. 1971-05-01 9:32:24:42
'U' writes full weekday name, day, fulll month, year, hour, minute ans second, e.g. Tuesday, 1 April 2008 18:07:05
'v' writes time hh/mm, e.g. 02:04
'V' writes time h/mm, e.g. 2:04
'w' writes 2 digit hour (24H), e.g. 07
'W' writes hour (24H), e.g. 7
'x' writes 2 digit hour (12H), e.g. 07
'X' writes hour (12H), e.g. 7
'y' writes full month name and year, e.g. October, 71 (locale dependent).
'Y' writes full month name and year, e.g. October, 1971 (locale dependent).
'z' writes time zone "local" or "UTC".
'Z' writes time zone "local" or "UTC".

Implements xtd::iformatable.