Linux下如何用date获取当前日期的前一天
2008-05-15 11:27
585 查看
今天遇到这个问题,从网上找到这两个合适的解答。
方法一:date命令的显示是与环境变量TZ有关的
$#看当前时区
$echo $TZ
CST-8
$#显示当前时间
$date
Mon Apr 2 15:48:36 CST 2002
$#改变当前时区,
TZ=CST+16;export TZ
$#显示当前时间(中间未改变系统时间,但date命令的显示已为昨天)
Mon Apr 1 15:48:33 CST 2002
=========================
方法二:一个详细的SCRIPT
#!/bin/sh
# ydate: A Bourne shell script that
# prints yestarday's date
# Output Form: Month Day Year
# From Focus on Unix: http://unix.about.com
# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`
# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`
# Subtract one from the current day.
day=`expr $day - 1`
# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then
# Find the preivous month.
month=`expr $month - 1`
# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`
# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi
# Print the month day and year.
echo $month $day $year
exit 0
============
如果是简单的只是想获得昨天的日期就很简单了,只要今天不是一号,呵呵
month=`date +%m`
day=`date +%d`
year=`date +%Y`
day=`expr $day - 1`
export DATEYMD=$year$month$day
echo $DATEYMD
方法一:date命令的显示是与环境变量TZ有关的
$#看当前时区
$echo $TZ
CST-8
$#显示当前时间
$date
Mon Apr 2 15:48:36 CST 2002
$#改变当前时区,
TZ=CST+16;export TZ
$#显示当前时间(中间未改变系统时间,但date命令的显示已为昨天)
Mon Apr 1 15:48:33 CST 2002
=========================
方法二:一个详细的SCRIPT
#!/bin/sh
# ydate: A Bourne shell script that
# prints yestarday's date
# Output Form: Month Day Year
# From Focus on Unix: http://unix.about.com
# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`
# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`
# Subtract one from the current day.
day=`expr $day - 1`
# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then
# Find the preivous month.
month=`expr $month - 1`
# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`
# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi
# Print the month day and year.
echo $month $day $year
exit 0
============
如果是简单的只是想获得昨天的日期就很简单了,只要今天不是一号,呵呵
month=`date +%m`
day=`date +%d`
year=`date +%Y`
day=`expr $day - 1`
export DATEYMD=$year$month$day
echo $DATEYMD
相关文章推荐
- js 的date的format时间,获取当前时间,前一天的日期
- 批处理:如何获取当前日期前一天或者后一天的日期
- linux下shell获取时间date的写法,日期加减,用date获得前一天的日期
- 如何用批处理文件写:获取当前日期的前一天
- 【HTML5入门】java获取util date类型当前系统前一天,当天,后天日期
- 如何用批处理文件写:获取当前日期的前一天
- java中获取当前日期和前一天日期
- java获取当前日期前一天和后一天
- linux date 如何获取前n周的自然周周一
- sql如何通过当前日期获取上周,上上周,上上上周的起始日期(周一_周七)
- 信息管理系统如何获取当前日期时间
- Java中Calendar的一些简单操作 及 JS中获取当前时间前一天时间(js中时间Date操作)
- [linux][Ubuntu]如何让date命令等地方显示英文日期
- perl在linux下通过date获取当前时间
- Java中Calendar的一些简单操作 及 JS中获取当前时间前一天时间(js中时间Date操作)
- matlab中如何获取当前日期时间
- matlab中如何获取当前日期时间
- (随笔)js获取当前时间并格格式化当前日期 获取date天后的日期(2014年11月27日 16:31:49)
- Linux如何获取循环日期
- 如何利用批处理文件获取前一天的日期