Core code
You can try these small functions when you encounter a system with a date command that does not give you power.
#日期转天数 function Date2days {echo "$*" | awk ' {z=int (14-$2)/12); y=$1+4800-z;
J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400)-2472633; Print J} '} Date2days ' echo ' 2010-08-18 18:59:19 | Sed ' s/-//g;s/://g ' #天数转日期 function days2date {echo ' $ | awk ' {a=$1+2472632; B=int ((4*a+3)/146097); C=int ((-
b*146097)/4 +a; D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c;
M=int ((5*e+2)/153); Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3;
Yy=b*100+d-4800+int (M/10); printf ("%4d-%02d-%02d\n", Yy,mm,dd)} '} days2date 14839 #日期转分钟 function date2minutes {echo ' $* ' | awk ' {z=int ( (14-$2)/12); Y=$1+4800-z;
m=$2+12*z-3;
J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400)-2472633; j=j*1440+$4*60+$5 print J} '} Date2minutes ' echo ' 2010-08-18 18:59:19 | Sed ' s/-//g;s/://g ' #分钟转日期 function minutes2date {echo ' $ | awk ' {i=$1; nn=i%60; I=int (I/60); hh=i%24; dd=in T (I/24);I=int (I/24); a=i+2472632; B=int ((4*a+3)/146097);
C=int ((-b*146097)/4) +a; D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c;
M=int ((5*e+2)/153); Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3;
Yy=b*100+d-4800+int (M/10); printf ("%4d-%02d-%02d%02d:%02d\n", Yy,mm,dd,hh,nn)} '} minutes2date 21369299 #日期转秒数 function date2seconds {echo $ *" |
awk ' {z=int (14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400)-2472633; j=j*86400+$4*3600+$5*60+$6 print J} '} Date2seconds ' echo ' 2010-08-18 18:59:19 | Sed ' s/-//g;s/://g ' #秒数转日期 function seconds2date {echo ' $ | awk ' {i=$1; ss=i%60; I=int (I/60); nn=i%60; i=int (I/60); hh=i%24; Dd=int (I/24);
I=int (I/24); a=i+2472632; B=int ((4*a+3)/146097);
C=int ((-b*146097)/4) +a; D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c;
M=int ((5*e+2)/153); Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3;
Yy=b*100+d-4800+int (M/10); printf ("%4d-%02d-%02d%02d:%02d:%02d\n", yy, Mm,dd,hh,nn,ss)} '} seconds2date 1282157959 #日期转毫秒 function date2milliseconds {echo ' $* ' | awk ' {z=int (14-$2 )/12); Y=$1+4800-z;
m=$2+12*z-3;
J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400)-2472633; j=j*86400+$4*3600+$5*60+$6 printf ("%d%s\n", J,$7)} '} Date2milliseconds ' echo ' 2010-08-18 18:59:19.073 ' | Sed ' s/-//g;s/://g;s/\.//g ' #毫秒转日期 function milliseconds2date {echo ' $ ' | awk ' {i=$1; ms=i%1000; I=int (i/100 0); ss=i%60; I=int (I/60); nn=i%60; I=int (I/60); hh=i%24; Dd=int (I/24);
I=int (I/24); a=i+2472632; B=int ((4*a+3)/146097);
C=int ((-b*146097)/4) +a; D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c;
M=int ((5*e+2)/153); Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3;
Yy=b*100+d-4800+int (M/10); printf ("%4d-%02d-%02d%02d:%02d:%02d.%0 3d\n ", Yy,mm,dd,hh,nn,ss,ms)} '} Milliseconds2date 1282157959073
application Example:
Calculates the date after today's n days
#!/bin/bash
function date2days {
echo "$ $" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
Print J
} '
}
function days2date {
echo ' | ' | awk ' {
a=$1+2472632; B=int (4*a+3)/146097); C=int (- b*146097)/4 +a;
D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c; M=int ((5*e+2)/153);
Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3; Yy=b*100+d-4800+int (M/10);
printf ("%4d%02d%02d\n", Yy,mm,dd)
} '
}
year= ' date +%y '; month= ' date +%m '; day= ' date +%d '
days= ' Date2days $year $month $day '
n=5 let
days-= $N
days2date $days
Calculates the date after the nth day of the day
#!/bin/bash
function date2days {
echo "$ $" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
Print J
} '
}
function days2date {
echo ' | ' | awk ' {
a=$1+2472632; B=int (4*a+3)/146097); C=int (- b*146097)/4 +a;
D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c; M=int ((5*e+2)/153);
Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3; Yy=b*100+d-4800+int (M/10);
printf ("%4d%02d%02d\n", Yy,mm,dd)
} '
}
year=2010 month=01 day=20
days= ' date2days $year $month $day ' let
days+=5
days2date $days
Calculate the full date of the last one weeks
#!/bin/bash function Date2days {echo "$ $" | awk ' {z=int (14-$2)/12); y=$1+ 4800-z;
m=$2+12*z-3;
J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400)-2472633;
Print J} '} function days2date {echo ' | ' | awk ' {a=$1+2472632; B=int (4*a+3)/146097); C=int ((-b*146097)/4) +a; D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c;
M=int ((5*e+2)/153); Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3;
Yy=b*100+d-4800+int (M/10); printf ("%4d%02d%02d\n", Yy,mm,dd)} '} function Date2week {echo "$ $" | awk ' {z=int (14-$2)/12); y=$1+4800 -Z;
m=$2+12*z-3;
dow= (int ((153*M+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400)-2472629)%7; Print Dow} '} year= ' date +%y '; Month= ' Date +%m '; day= ' Date +%d ' days= ' date2days $year $month $day ' week= ' date2week $year $month $day ' let dateend= $days-$week-1 Let Datebeg In= $dateEnd-6 for ((i= $dateBegin; i<= $dateEnd; i++)); Do days2date $i done
Date time converted to milliseconds
function Date2milliseconds {
echo "$*" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
j=j*86400+$4*3600+$5*60+$6
print j$7
} '
}
date2milliseconds ' echo ' 2010-08-18 18:59:19.073 ' |/usr/ Xpg4/bin/awk-f ' [:.-]+ ' $1=$1 '
Date time converted to seconds
function Date2seconds {
echo "$*" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
j=j*86400+$4*3600+$5*60+$6
print J
} '
}
date2seconds ' echo ' 2010-07-21 00:00:00 ' | sed ' s/-//g;s/ ://g '
Judge whether a number is a valid date
function Date2days {
echo "$*" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
Print J
} '
}
function days2date {
echo ' | ' | awk ' {
a=$1+2472632; B=int (4*a+3)/146097); C=int (-B *146097)/4 +a;
D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c; M=int ((5*e+2)/153);
Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3; Yy=b*100+d-4800+int (M/10);
printf ("%4d%02d%02d\n", Yy,mm,dd)
} '
}
num1=20105050
num2=20101001
arg1= ' echo ' $num 1 ' | Sed-r ' s/(...) (..) (..) /\1 \2 \3/g '
arg2= ' echo ' $num 2 ' | sed-r ' s/(...) (..) (..) /\1 \2 \3/g '
days1= ' date2days $arg 1 '
date1= ' days2date $days 1 '
days2= ' date2days $arg 2 '
date2= ' Days2date $days 2 '
["$num 1"-eq "$date 1"] && echo "$num 1 is valid date" | |-echo "$num 1 is invalid date"
["$num 2"-eq "$date 2"] && echo "$num 2 is valid date" | | echo "$num 2 is invalid date"
Calculate the time before 10 minutes
function date2minutes {
echo "$*" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
J=j*1440+$4*60+$5
print J
} '
}
function minutes2date {
echo ' \ | awk ' {
i=$1; nn=i%60; i=int (I/60); hh=i%24; Dd=int (I/24); I=int (I/24);
a=i+2472632; B=int ((4*a+3)/146097); C=int ((-b*146097)/4) +a;
D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c; M=int ((5*e+2)/153);
Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3; Yy=b*100+d-4800+int (M/10);
printf ("%4d-%02d-%02d%02d:%02d\n", Yy,mm,dd,hh,nn)
} '
}
now= ' date ' +%y-%m-%d%h:%m ' | sed ' s/-//g;s/ ://g '
minutes= ' date2minutes $now ' let
minutes-=10
minutes2date $minutes
Calculates the number of days between a specified date and the current system date
#!/bin/bash
function date2days {
echo "$*" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
Print J
} '
echo ' Enter your date: "
read input
inpudays=$ (date2days ${input:0:4} ${input:4:2} ${ Input:6:2})
sysdays=$ (date2days ' date +%Y%m%d ') let
result= $InpuDays-$SysDays
Echo $result
#./test.sh
Enter Your date:
20110605
25
The date of Monday last week
#!/bin/bash
function date2days {
echo "$ $" | awk ' {
z=int (14-$2)/12);
y=$1+4800-z; J=int ((153*m+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472633;
Print J
} '
}
function days2date {
echo ' | ' | awk ' {
a=$1+2472632; B=int (4*a+3)/146097); C=int (- b*146097)/4 +a;
D=int ((4*c+3)/1461); E=int (( -1461*d)/4) +c; M=int ((5*e+2)/153);
Dd=-int ((153*m+2)/5) +e+1; Mm=int (-M/10) *12+m+3; Yy=b*100+d-4800+int (M/10);
printf ("%4d%02d%02d\n", Yy,mm,dd)
} '
}
function Date2week {
echo ' $ $ ' | awk ' {
z=int (14 -$2)/12); Y=$1+4800-z; m=$2+12*z-3;
dow= (int ((153*M+2)/5) +$3+y*365+int (Y/4)-int (y/100) +int (y/400) -2472629)%7;
Print Dow
} '
}
year= ' date +%y '; month= ' date +%m '; day= ' date +%d ' days= ' date2days
$year $month $day '
week= ' date2week $year $month $day ' let
date= $days-$week-7
days2date $date