给一个日期,判断这个日期是当年的多少天(shell实现)

该博客介绍了如何使用Shell脚本来实现根据给定的年、月、日,计算这个日期在当年的第几天。内容包括具体的Shell代码实现,以及如何保存和运行代码的说明。示例中,2016年1月3日是当年的第3天,而2016年2月1日是第32天。
摘要由CSDN通过智能技术生成

shell实现给定一个日期得到具体天数

Question

Given a specific date, write a function to return the day of the year for that date. The signature of the function is
int dayOfYear(int year, int month, int day)

For example, given input of year=2016, month=1, day=3. The function should return 3 as the date 2016-01-03 is the 3rd day of the year 2016.
Another example, given input of year=2016, month=2, day=1. The function should return 32 as the date 2016-02-01 is the 32nd day of the year 2016.

Notes:

Please write your own algorithm. Do NOT use any class like Calendar in Java.

Please give a full implementation. Do NOT write pseudo code.

Please include your own logic of determining a leap year.

You are free to choose any programming language, not limited to Java, C# or C++. Please tell us the programming language you used in your answer.

shell 代码

#!/bin/bash
# Harlen
# 2022/03/22
# 此脚本功能为用户输入年月日,计算出这个日期是当年的多少天

# 获取用户输入date
while :
do
  read -p "请输入年份:" Y
  # 判断号数输入是否合法,号数输入必须是非空整数且>0!!
  if [ -n $Y ] && [[ $Y =~ ^[0-9]+$ ]] && [ $Y -gt 0 ];then
    break
  else
    echo -e "\033[31m输入错误请重新输入 \033[0m"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值