Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Wednesday, December 30, 2020

how to convert from one date format to another format in linux ?

 how to convert from one date format to another format in linux ?

echo 04-11-2021 | { IFS=- read d m y && echo "$y$m$d"; }

20211104


echo 04-11-2021 | { IFS=- read d m y && echo "$y/$m/$d"; }

2021/11/04


 echo 04-11-2021 | { IFS=- read d m y && echo "$d$m$y"; }

04112021


 echo 04-11-2021 | awk -F- '{print $3$2$1}'

20211104


echo 04-11-2021 |gawk -F, '{split($1, a, "-"); print a[3] a[2] a[1]}' 

20211104


echo 04/11/2021 | { IFS=/ read d m y && echo "$y$m$d"; }

20211104


date -d "20211104" "+%Y-%m-%d"

2021-11-04


date -d 04/11/2021 +%F

2021-04-11


 %F   full date; same as %Y-%m-%d


Wednesday, October 9, 2019

How to Convert Epoch Seconds To the Current Time in linux?

Linux / UNIX: Convert Epoch Seconds To the Current Time

how you can obtain the UNIX epoch time (number of seconds since 1970-01-01
00:00:00 UTC) using the Linux bash "date" command. It also shows how you can
convert a UNIX epoch time to a human readable time.

Obtain UNIX epoch time using bash

Obtaining the UNIX epoch time using bash is easy. Use the build-in date command and
instruct it to output the number of seconds since 1970-01-01 00:00:00 UTC. You can do this
by passing a format string as parameter to the date command. The format string for UNIX
epoch time is '%s'.


Print Current UNIX Time
Type the following command to display the seconds since the epoch:

Syntax: date +%s

date +%s
1569569962


Convert Epoch To Current Time
Type the command:

date -d @Epoch

date -d @1569569962
date -d "1970-01-01 1569569962 sec GMT"

Sample outputs:

Fri Sep 27 13:09:22 IST 2019

To convert number of seconds back to a more readable form, use a command like this:

date -d @1569569962 +"%d-%m-%Y %T %z"


Using AWK



awk 'BEGIN { print strftime("%Y-%m-%d %H:%M:%S", 1569569962); }'

Output:

2019-09-27 13:09:22

Using Perl

perl -e 'print scalar(localtime(1569569962)), "\n"'

Output:

Fri Sep 27 13:09:22 2019

Tuesday, January 1, 2019

How to count days since a specific date until today using Bash shell ?


The easiest way to perform days count since a specifics day is to first get a number of seconds
since epoch time ( 1970-01-01 ) for both dates. As an example let’s count number of days
since 28.12.1999 until today 8.1.2018. Consider a following example:
$ echo $((($(date +%s)-$(date +%s --date "1999-12-28"))/(3600*24))) days
6586 days

Let's add little bit of readability to the above command by using variables. First, we get
seconds since epoch time ( 1970-01-01 ) until now:

$ now=$(date +%s)

$ echo $now
1515370378

Next we do the same for the 28.12.1999 date:

past=$(date +%s --date "1999-12-28")
$ echo $past
946299600

Next, calculate the difference:

$ difference=$(($now-$past))
$ echo $difference
569070778

Lastly, convert the difference in seconds to days:
$ echo $(($difference/(3600*24)))
6586

All done. The same principle can be used to calculate days between any specific days.
For example let's count days between 1.1.2016 and 31.12.2016 dates:

$ echo $((($(date +%s --date "2016-12-31")-$(date +%s --date "2016-1-1"))/(3600*24))) days
365 days