给定日期,获取星期几 - SYSTEMTIME

发布于 2024-09-05 00:01:57 字数 118 浏览 1 评论 0 原文

如果提供了日期(月-日-年),是否可以使用 SYSTEMTIME 确定星期几,或者该结构只是单向的?

如果 SYSTEMTIME 无法做到这一点(使用 Win32),完成我所要求的任务的最轻量级方法是什么?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开雨落又逢春i 2024-09-12 00:01:57

根据 msdnSYSTEMTIME 转换为 FILETIME 时,>wDayOfWeek 成员将被忽略。转换回来的时候就填了。

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

WORD dayofweek = t.wDayOfWeek;

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;
今天小雨转甜 2024-09-12 00:01:57

另一种可能更加独立于平台的方法是使用 localtime< /a> 或 gmtime

例如,打印当前星期几:

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 );

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 );
凉宸 2024-09-12 00:01:57

使用 SystemTimeToFileTime 将 SYSTEMTIME 转换为 FILETIME。然后使用 FileTimeToSystemTime 将其转换为带有星期几的 SYSTEMTIME。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文