Skip to content

Date-Time

A snippet that formats the current date and time with GetDateFormat and GetTimeFormat.

Published Aug 20, 1996

The Win32 way to get locale-aware date and time strings: fetch a SYSTEMTIME with GetLocalTime, then run it through GetDateFormat (DATE_LONGDATE or DATE_SHORTDATE) and GetTimeFormat (TIME_NOSECONDS), all in LOCALE_USER_DEFAULT so the output matches the user's regional settings. The example prints them combined as "time, long date (short date)". Use it whenever you need display-ready date text without hand-rolling month names or separators.

#include <windows.h>
#include <stdio.h>

static  SYSTEMTIME  st;
static  char   d [257];
static  char   t [257];
static  char   s [257];

int main(int argc, char *argv[])
{
GetLocalTime(&st);
GetDateFormat(LOCALE_USER_DEFAULT,DATE_LONGDATE,&st,NULL,d,64);
GetDateFormat(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&st,NULL,s,64);
GetTimeFormat(LOCALE_USER_DEFAULT,TIME_NOSECONDS,&st,NULL,t,64);
printf("%s%s%s%s%s%s\n",t,", ",d," (",s,")");
return 0;
}
An unhandled error has occurred. Reload 🗙