-3

Some body share sample code to get my local system date format using cpp. For example I need to get system date format is whether 'dd-mm-yyyy' or 'mm-dd-yyyy' or 'yyyy-mm-dd'

I just need to check the system date format only. No need to get date, month and year. Kindly assist me.

kittu
  • 6,662
  • 21
  • 91
  • 185
sivanesan1
  • 779
  • 4
  • 20
  • 44
  • Possible duplicate of [How to get current time and date in C++?](https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c) – ichherzcplusplus Oct 10 '19 at 12:18
  • 1
    @ichherzcplusplus: i need to get already sets local time format whether 'dd-mm-yyyy' or 'mm-dd-yyyy' or 'yyyy-mm-dd'. In that above example i can get date and time of system of what ever format i required. But i need to get in which format the date and time which already sets in my system – sivanesan1 Oct 10 '19 at 12:24
  • Possible duplicate of [How do I get the current “localized pattern” for the date and time of an std::locale](https://stackoverflow.com/questions/34750954/how-do-i-get-the-current-localized-pattern-for-the-date-and-time-of-an-stdlo). – Eljay Oct 10 '19 at 12:48
  • Most questions starting with "How..." are too broad. What is your specific question? Where do you have trouble? What have you tried so far? – Thomas Sablik Oct 10 '19 at 12:50
  • I tried looking in Howard Hinnants [date](https://github.com/HowardHinnant/date) library to see how he handles `%x` (the localized date representation) but I think it requires too much digging for me. This might help even though it doesn't say if there should be `-` or `/` between the entities: : https://en.cppreference.com/w/cpp/locale/time_get/date_order – Ted Lyngmo Oct 10 '19 at 12:58

1 Answers1

2

Assuming the locale is set up properly, you can use:

https://en.cppreference.com/w/cpp/locale/time_get/date_order

Returns a value of type std::time_base::dateorder, which describes the default date format used by this locale (expected by get_date() and produced by std::strftime() with format specifier '%x').

std::time_base::dateorder d = std::use_facet<std::time_get<char>>(
                                       std::locale()
                              ).date_order();
switch (d)
{
    case std::time_base::no_order: std::cout << "no_order\n"; break;
    case std::time_base::dmy: std::cout << "day, month, year\n"; break;
    case std::time_base::mdy: std::cout << "month, day, year\n"; break;
    case std::time_base::ymd: std::cout << "year, month, day\n"; break;
    case std::time_base::ydm: std::cout << "year, day, month\n"; break;
}

NOTE: This function is optional, it may return no_order in every case.

Unfortunately I can only get it to print no_order. I'm not sure if this is a problem with the compiler, locale, or system I'm running it on. The example on cppreference gives me no_order for everything so my guess is the compiler (I've tried gcc and clang) hasn't implemented it.

There may be system-specific calls that will give you what you want.

Kevin
  • 6,993
  • 1
  • 15
  • 24