Home > Software engineering >  date format of a local system in cpp
date format of a local system in cpp

Time:02-18

we can configure the local system format in D/M/YYYY or DD/MM/YYYY .in my project we have to get the local date format and show it in the list box as it is. i am configured my machine as DD/MM/YYYY and we are using CTime function to get the current time from the system but CTime returns only single digit of day(example:2/2/2022 instead of 02/02/2022)from 1 to 9th day of every month.is there any alternative function to get the date format same as system date format or any alternate function to get the proper date format(we are using visual studio with c platform). below is the sample code where day comes in single digit for the 1st to 9th day of every month

 now= ctime(&timer);
 sscanf(now, "%s %s %s %s %s\n", w, month, day, time, year);

for the date 02/02/2022 if the local system format is DD/MM/YYY then day in the scanf statment should be 02 and if the local system format is D/M/YYY then day in the scanf statement should be 2. Please suggest

Thanks in advance

CodePudding user response:

Here https://docs.microsoft.com/en-us/windows/win32/api/datetimeapi/nf-datetimeapi-getdateformata you have the specification for the getdateformat function, using that function you could get the format for the local system. Then you can format your output properly using %s or %.2s to force one zero padding for 0-9 values.

CodePudding user response:

Use strftime with %D format string.

  • Related