# How to convert between date formats on the command line



## DominikHoffmann (Feb 1, 2012)

How would I convert a string, such as

```
2012-01-31 18:54:55 +0000
```
into the format of the

```
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
```
i.e., Epoch time?


----------



## Viro (Feb 3, 2012)

Try this:

```
date --date='2012-01-31 18:54:55 +0000' +'%a %b %d %T %Z %Y'
```

That code produces "Tue Jan 31 18:54:55 GMT 2012" which is presumably what you're after.


----------



## DominikHoffmann (Feb 3, 2012)

That command produces the output

```
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
```


----------



## Viro (Feb 3, 2012)

Hmm... it looks like the shell utilities that are shipped with OS X aren't standard. Sorry I can't help more as I don't have my Mac with me.


----------



## DominikHoffmann (Feb 3, 2012)

Viro said:


> Try this:
> 
> ```
> That code produces "Tue Jan 31 18:54:55 GMT 2012" which is presumably what you're after.[/QUOTE]
> ...


----------



## BjarneDM (Feb 3, 2012)

You'll have to combine the information from *man 1 date* and *man 3 strftime*


```
declare myDate="2012-01-31 18:54:55 +0000"
date -j -f "%Y-%m-%d %T %z" "${myDate}" "+%s"
```


----------

