ConvertDateToCTime

How to convert a date to ctime

Here is a strange question, that I received today: "How do you convert a date into ctime?". The ctime value being, of course, the number of seconds from the UNIX "Epoch".

It turns out the GNU date command is very helpful in that regard:

[root@galactus ~]# date ; date +%s
lun nov 14 18:18:11 CET 2011
1321291091

But how do you convert, say, yesterday's date into ctime? Simple:

[root@galactus ~]# date --date "yesterday" +%s
1321204758

Interesting, isn't it? Now, the example above gives you the ctime for Nov. 13th 2011 at 00:00 precisely. What about a different time?

[root@thot ~]# date --date "2011/11/13 16:00" +%s
1321196400

So, there you have it: date to ctime in no time! (Sorry, that joke was a bit too easy, even for me...)

See Also: