# How to get second or days expire for some date?



## andrian (Apr 22, 2020)

Hi, please help me.
I use date utilits for get second or days to expire for some date.
Example, i want to get second to New Year 2021?

```
root@monitor:/home/andrian # date
Wed Apr 22 16:15:27 EEST 2020
root@monitor:/home/andrian # date +%s
1587562219
```


----------



## DutchDaemon (Apr 22, 2020)

https://www.epochconverter.com/#tools and https://www.epochconverter.com/timestamp-list have plenty of tips and examples.


----------



## andrian (Apr 22, 2020)

Thank you for the link.
How get  seconds  from "Wed Apr 22 16:15:27 EEST 2021" ?

```
root@monitor:/home/andrian # date +%s -d "Wed Apr 22 17:19:39 EEST 2021"
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds|file] [-t west] [-v[+|-]val[ymwdHMS]]
            [-I[date | hours | minutes | seconds]]
            [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]
```


----------



## SirDice (Apr 22, 2020)

Use date(1)  to convert the date/time to epoch seconds. Then you can do simple arithmetic to add/subtract dates. Convert the epoch seconds back to weeks, days and hours.


----------



## andrian (Apr 22, 2020)

pyret said:


> ```
> $ echo "Wed Apr 22 16:15:27 EEST 2021" |awk -F: '{print substr($3,1,2)}'
> 27
> ```
> Is that what you're after?


No!

```
root@monitor:/home/andrian # date +%s
1587566566
```
This seconds for now date:

```
root@monitor:/home/andrian # date
Wed Apr 22 17:43:30 EEST 2020
```
So, i want get seconds for date "Wed Apr 22 17:43:30 EEST 202*1*"? Namely:

```
# date +%s -j -d Wed Apr 22 17:19:39 EEST 2021
and get 1658756656
```


----------



## SirDice (Apr 22, 2020)

Can't  get  it to work with the timezone for some  reason:

```
% date -j -f "%a %b %d %T %Y" "Wed Apr 22 17:43:30 2021" "+%s"
1619106210
```
Adding the timezone fails:

```
% date -j -f "%a %b %d %T %Z %Y" "Wed Apr 22 17:43:30 EEST 2021" "+%s"
Failed conversion of ``Wed Apr 22 17:43:30 EEST 2021'' using format ``%a %b %d %T %Z %Y''
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds|file] [-t west] [-v[+|-]val[ymwdHMS]]
            [-I[date | hours | minutes | seconds]]
            [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]
```


----------



## andrian (Apr 22, 2020)

SirDice said:


> Can't  get  it to work with the timezone for some  reason:
> 
> ```
> % date -j -f "%a %b %d %T %Y" "Wed Apr 22 17:43:30 2021" "+%s"
> ...


So! Thank you! You super! What you need!


----------



## obsigna (Apr 22, 2020)

Use the numeric equivalent for the timezone. EEST = +0300

`date -jf "%a %b %d %T %z %Y" "Wed Apr 22 17:43:30 +0300 2021" "+%s"`


----------



## SirDice (Apr 22, 2020)

Yes, unless EEST isn't a valid timezone name I would think %Z should be able to convert it though.


```
%Z    is replaced by the time zone name.

     %z    is replaced by the time zone offset from UTC; a leading plus sign
           stands for east of UTC, a minus sign for west of UTC, hours and
           minutes follow with two digits each and no delimiter between them
           (common form for RFC 822 date headers).
```

Edit: It might have  something to do with daylight-savings. More specifically when this switch happens:

```
dice@hosaka:~ % date -j -f "%a %b %d %T %Z %Y" "Wed Apr 22 17:43:30 CEST 2021" "+%s"
1619106210
dice@hosaka:~ % date -j -f "%a %b %d %T %Z %Y" "Wed Apr 22 17:43:30 CET 2021" "+%s"
Failed conversion of ``Wed Apr 22 17:43:30 CET 2021'' using format ``%a %b %d %T %Z %Y''
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds|file] [-t west] [-v[+|-]val[ymwdHMS]]
            [-I[date | hours | minutes | seconds]]
            [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]
```

CET would be the wrong timezone for 22-April.


----------

