How to Set Just Year with Linux Date Command

Get the current year on the commandline

You can simply use the date command as date +%Y:

wget "http://example.com/data-$(date +%Y).txt"

If you need the 2-digit year, you can simply use date +%y.

YYYY-MM-DD format date in shell script

In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external date (usually GNU date).

As such:

# put current date as yyyy-mm-dd in $date
# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided
# -2 -> start time for shell
printf -v date '%(%Y-%m-%d)T\n' -1

# put current date as yyyy-mm-dd HH:MM:SS in $date
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1

# to print directly remove -v flag, as such:
printf '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal

In bash (<4.2):

# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')

# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')

# print current date directly
echo $(date '+%Y-%m-%d')

Other available date formats can be viewed from the date man pages (for external non-bash specific command):

man date

How do I get the first day of the given year's day of the week in Bash Shell?

Please see the below code which will give you 1 first day of the year passed:

# !/usr/bin/bash
year=2020
firstday=`date -d "01 Jan $year" +'%A,%d'`
echo "First day of the year '$year' is : '$firstday'"

Output:
First day of the year '2020' is : 'Wednesday,01'
If you want to just get the day , then remove the %d option.

firstday=`date -d "01 Jan $year" +'%A`

Output :First day of the year '2020' is : 'Wednesday'

Using `date` command to get previous, current and next month

The problem is that date takes your request quite literally and tries to use a date of 31st September (being 31st October minus one month) and then because that doesn't exist it moves to the next day which does. The date documentation (from info date) has the following advice:

The fuzz in units can cause problems with relative items. For
example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:

 $ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!

Command to get time in milliseconds

date +%s%N returns the number of seconds + current nanoseconds.

Therefore, echo $(($(date +%s%N)/1000000)) is what you need.

Example:

$ echo $(($(date +%s%N)/1000000))
1535546718115

date +%s returns the number of seconds since the epoch, if that's useful.

Extract month and day from linux date command

To read the month and day into shell variables (assuming bash/ksh/zsh)

read month day < <(date -d "2 days" "+%m %d")

If you're planning to do arithmetic on these values, be of numbers that would be treated as octal but are invalid octal numbers 08 and 09. If you want to strip off the leading zero, use "+%_m %_d"


Using read, the shell takes care of the excess whitespace for you:

read mon day year < <(date -d "2 days" "+%b %_d %Y")
testdate="$mon $day $year"

If you don't want to use the temp vars:

testdate="Apr  5 2014"       # $(date -d "2 days" "+%b %_d %Y")
testdate=${testdate// / } # globally replace 2 spaces with 1 space
echo "$testdate"

How to increment a date in a Bash script

Use the date command's ability to add days to existing dates.

The following:

DATE=2013-05-25

for i in {0..8}
do
NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")
echo "$NEXT_DATE"
done

produces:

05-25-2013
05-26-2013
05-27-2013
05-28-2013
05-29-2013
05-30-2013
05-31-2013
06-01-2013
06-02-2013

Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day").

Add some specific time while using the linux command date

On Linux

Just use -d (or --date) to do some math with the dates:

date -d '+1 hour' '+%F %T'
# ^^^^^^^^^^^^

For example:

$ date '+%F %T'
2013-04-22 10:57:24
$ date -d '+1 hour' '+%F %T'
2013-04-22 11:57:24
# ^

On Mac OS

Warning, the above only works on Linux, not on Mac OS.

On Mac OS, the equivalent command is

date -v+1H


Related Topics



Leave a reply



Submit