免费注册 查看新帖 |

Chinaunix

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

如何计算一个日期是星期几?  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2002-08-11 15:16 |只看该作者 |正序浏览
[这个贴子最后由valentine在 2002/08/11 03:17pm 编辑]

本来有这样的一个讨论的帖子,不小心被俺删掉了.觉得有些可惜,便写了一个贴上来.

#!/bin/ksh
getdayofweek()
{
    #  Function dayofweek
    #  Sample input:20020703

    #  Extract the month, day, and year
    year=`echo $1|cut -c1-4`
    month=`echo $1|cut -c5-6`
    day0=`echo $1|cut -c7-8`
    day=${day0#0}
   

    #  Get the calendar line for the specified day of the month/year
    dline=`cal $month $year | sed 's// X /g&#59; s/^/ /&#59; s/$/ /' | grep " $day "`

    #  Compute the day of the week
    IFS='
'
    set $dline
    dow=1    for weekday do
        if [[ $day = $weekday ]]
        then
            break
        else
            (( dow = dow+1 ))
        fi
    done

    #  Convert the numeric day of the week to a string
    case $dow in
        1) print Sun &#59;&#59;
        2) print Mon &#59;&#59;
        3) print Tue &#59;&#59;
        4) print Wed &#59;&#59;
        5) print Thu &#59;&#59;
        6) print Fri &#59;&#59;
        7) print Sat &#59;&#59;
        *) print "wrong date" &#59;&#59;
    esac
}

euler 该用户已被删除
28 [报告]
发表于 2007-01-27 15:03 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
27 [报告]
发表于 2006-12-23 23:34 |只看该作者
原帖由 sham 于 2002-8-14 01:16 发表
这样就可以了
$ date -d "2002/08/13" "+%a"

还是这个命令来的简单

论坛徽章:
0
26 [报告]
发表于 2003-06-19 11:34 |只看该作者

如何计算一个日期是星期几?

原帖由 "valentine"]最后由valentine在 2002/08/21 12:03pm 编辑 发表:


我测试可以了,又安照偶的习惯改了一个valentine版.
get_whatdays()
{
year=`echo $1|cut -c1-4`
month=`echo $1|cut -c5-6`
day=`echo $1|cut -c7-8`

b=`cal $month $..........
   

用print是不是函数就没有返回值了???要让函数有返还值,必须用echo嘛?

论坛徽章:
0
25 [报告]
发表于 2003-03-27 09:15 |只看该作者

如何计算一个日期是星期几?

get_whatdays()
{
yy=`echo $1|cut -c 1-4`
mm=`echo $1|cut -c 5-6`
dd=`echo $1|cut -c 7-8`
expr $dd : 0. > /dev/null && dd=`echo $dd|cut -c 2-2`
aaa=`eval cal $mm $yy|grep " 20 "|cut -c 1-2`
dd=`expr \( $dd - $aaa \) \% 7`
[ $dd -lt 0 ] && dd=`expr $dd + 7`
echo $dd
}

复制都能搞错,偶也真够春的

论坛徽章:
0
24 [报告]
发表于 2003-03-27 09:12 |只看该作者

如何计算一个日期是星期几?

date --date='20030328' +%a 是什么用法?

论坛徽章:
0
23 [报告]
发表于 2003-03-27 09:11 |只看该作者

如何计算一个日期是星期几?

get_whatdays()
{
yy=`echo $1|cut -c 1-4`
mm=`echo $1|cut -c 5-6`
dd=`echo $1|cut -c 7-8`
expr $dd : 0. > /dev/null && dd=`echo $dd|cut -c 2-2`
aaa=`eval cal $mm $yy|grep " 20 "|cut -c 1-2`
dd=`expr $dd - $aaa \% 7`
[ $dd -lt 0 ] && dd=`expr $dd + 7`
echo $dd
}

论坛徽章:
0
22 [报告]
发表于 2003-03-27 07:51 |只看该作者

如何计算一个日期是星期几?

date --date='20030328' +%a
返回和语言相对应的值,如果是中文,就是一二三四五等。

论坛徽章:
0
21 [报告]
发表于 2002-08-23 23:14 |只看该作者

如何计算一个日期是星期几?

高手就是高手,就是不一样的感觉!!

论坛徽章:
0
20 [报告]
发表于 2002-08-21 12:02 |只看该作者

如何计算一个日期是星期几?

那应该是你说的对.我把它改过来.
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP