Given Date, Get Day of Week - SYSTEMTIME

空扰寡人 提交于 2019-12-07 00:33:28

问题


Is it possible to determine the day of the week, using SYSTEMTIME, if a date (month-day-year) is provided or is this structure one-way only?

What is the most lightweight way to accomplish what I am asking if SYSTEMTIME cannot do it (using Win32)?


回答1:


According to the msdn, the wDayOfWeek member is ignored when converting SYSTEMTIME to FILETIME. When converting back, it's filled in.

SYSTEMTIME t = { 2010, 6, -1 /*ignored*/, 11 };
FILETIME ft;
HRESULT hrto   = SystemTimeToFileTime( &t, &ft );
HRESULT hrback = FileTimeToSystemTime( &ft, &t );

WORD dayofweek = t.wDayOfWeek;



回答2:


Another way of doing it that might be a bit more platform independent would be to use localtime or gmtime.

For example, print current day of week:

struct tm *timeval;
time_t tt;
tt = time( NULL );
timeval = localtime( &tt );
// print zero based day of week
printf( "day of week = %d\n", timeval->tm_wday );



回答3:


Use SystemTimeToFileTime to covert the SYSTEMTIME to a FILETIME. Then use FileTimeToSystemTimeto convert it to a SYSTEMTIME with day of week.



来源:https://stackoverflow.com/questions/3017745/given-date-get-day-of-week-systemtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!