免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4819 | 回复: 11
打印 上一主题 下一主题

如何取一个日期所对应是本年的第几周,星期几? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-14 15:32 |只看该作者 |倒序浏览
不知道如何操作,对于时间函数,之前用到的只是 localtime 这个函数。也不知道有哪些函数可以用的,但在 time 结构体里在,我似乎没有看到星期几的标签。不知道大家是否有这样的需求,需求出现的时候是如何解决的。

论坛徽章:
0
2 [报告]
发表于 2011-01-14 15:42 |只看该作者
http://bbs.chinaunix.net/thread-1846032-1-1.html
把这个代码下下来看看吧 很全的

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
3 [报告]
发表于 2011-01-14 15:48 |只看该作者
localetime返回的指针里面有 星期几,第几周通过day of year自己计算下

论坛徽章:
0
4 [报告]
发表于 2011-01-14 16:04 |只看该作者
二楼兄弟给的东西很好,可惜没有我要的。呵呵,
楼上的,周头尾的问题,很难解决。

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
5 [报告]
发表于 2011-01-14 16:08 |只看该作者
二楼兄弟给的东西很好,可惜没有我要的。呵呵,
楼上的,周头尾的问题,很难解决。
Advanceer 发表于 2011-01-14 16:04



    计算下同年元旦是星期几呀,比较下就能算出来了吧

论坛徽章:
0
6 [报告]
发表于 2011-01-14 20:33 |只看该作者
man strftime,如果我没记错的话,Linux的系统时间就是通过这个函数实现的

论坛徽章:
0
7 [报告]
发表于 2011-01-17 09:35 |只看该作者
有个 高斯 发明的公式,可以直接算出 星期几。

论坛徽章:
0
8 [报告]
发表于 2011-01-17 09:48 |只看该作者
SYNOPSIS
       #include <time.h>

       size_t strftime(char *s, size_t max, const char *format,
                       const struct tm *tm);

DESCRIPTION
       The  strftime() function formats the broken-down time tm according to the format specification format and places the
       result in the character array s of size max.

       The format specification is a null-terminated string and may contain special character sequences  called  conversion
       specifications,  each  of  which  is introduced by a '%' character and terminated by some other character known as a
       conversion specifier character.  All other character sequences are ordinary character sequences.

       The characters of ordinary character sequences (including the null byte) are copied verbatim from format to s.  How‐
       ever, the characters of conversion specifications are replaced as follows:

       %a     The abbreviated weekday name according to the current locale.

       %A     The full weekday name according to the current locale.

论坛徽章:
0
9 [报告]
发表于 2011-01-17 14:45 |只看该作者
首先得到元旦时周几,和所求日期的和元旦差多少天,取模7,不就得出了?

论坛徽章:
1
CU十二周年纪念徽章
日期:2013-10-24 15:41:34
10 [报告]
发表于 2011-01-17 16:45 |只看该作者
有个 高斯 发明的公式,可以直接算出 星期几。
logicBaby 发表于 2011-01-17 09:35


是这个吧:
  1. char *name[] = { "monday", "tuesday", "wednesday",
  2. "thursday", "friday", "saturday", "sunday" };
  3. int main(void)
  4. {
  5.         int d, m, y, a;
  6.         printf("Day: "); scanf("%d",&d);
  7.         printf("Month: "); scanf("%d",&m);
  8.         printf("Year: ");  scanf("%d",&y);
  9.         // 1月2月当作前一年的13,14月
  10.         if (m == 1 || m == 2) { m += 12; y--; }
  11.         // 判断是否在1752年9月3日之前
  12.     if ((y < 1752) || (y == 1752 && m < 9) ||
  13.                 (y == 1752 && m == 9 && d < 3))
  14.                 a = (d + 2*m + 3*(m+1)/5 + y + y/4 +5) % 7;
  15.         else
  16.                 a = (d + 2*m + 3*(m+1)/5 + y + y/4 - y/100 + y/400)%7;
  17.         printf("it's a %s\n", name[a]);
  18.         return 0;
  19. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP