Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a date from Unix time to string, in a script?

Tags:

bash

To convert a date from string to Unix time, I can use date --date "2012-02-13" +%s.

How do I do the opposite, from Unix time to string?

like image 609
user121196 Avatar asked Mar 23 '12 00:03

user121196


People also ask

How do I convert a date timestamp to date?

We can convert date to timestamp using the Timestamp class which is present in the SQL package. The constructor of the time-stamp class requires a long value. So data needs to be converted into a long value by using the getTime() method of the date class(which is present in the util package).


1 Answers

You can use date --date @datetime:

[foo@bar ~]$date --date "2012-02-13" +%s 1329055200 [foo@bar ~]$date --date @1329055200 Mon Feb 13 00:00:00 EST 2012 [foo@bar ~]$date --date @1329055200 +"%Y-%m-%d" 2012-02-13 

I'm not sure where the '@xxxx' is documented though!

like image 70
mathematical.coffee Avatar answered Sep 21 '22 07:09

mathematical.coffee