New answers tagged

1

Assuming GNU date run_date=20210615 # extract the year and month, and set the day as 01, then subtract 1 day endOfLastMonth=$(date -d "${run_date:0:4}-${run_date:4:2}-01 - 1 day" "+%Y-%m-%d")


0

First, Unix filesystems (FS) do not generally track the "creation time" (I say generally because there are now FS's that track the "birth" time). The "ctime" field from the stat() system call is not "(C)reation time" like in DOS/WIN, but rather it is the "inode (C)hange time"--the time the inode itself was ...


2

The -d option is not a standard option to date, and the standard date specification has no provision for taking a date in one format and reformat it in another format. BSD, GNU, ast-open and busybox date have options for that, but with different syntax and of the 4, GNU date, which you seem to be using given the wording of your error message, is the only ...


0

There's also dateutils with strptime. Just: $ strptime -i '%d/%m/%Y %H:%M' -e -f '%s\n' <<<'23/07/2021 14:44' 1627051440


1

The GNU date (and others) is not able to accept a date string in the format you want it. It mostly accept US date formats. To have a variable format, the easiest solution is to use busybox date, it just needs to be told the format (which you already have: %d/%m/%Y %H:%M) in the -D option: $ busybox date -u +%s -D '%d/%m/%Y' -d "23/07/2021" ...


3

You could use string manipulation to force the input into a format recognized by date. Or, perl where you must specify the exact input format. start='23/07/2021 14:44' perl -MTime::Piece -sE ' say Time::Piece->strptime($input, "%d/%m/%Y %H:%M")->epoch ' -- -input="$start" 1627051440 string manipulation: I'm using read to break ...


-1

A quick and lean solution that uses only sed: echo 19-07-2021 | sed -E 's/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/\3-\2-\1/'


4

Thanks all you guys for pitching in with various solutions. I believe few of the suggested solutions would work out for me as I correlate them to the approach I used in solving it on my own yesterday, right after posting this question. I used IFS to achieve it. IFS=- read d m y <<<'19-7-2021' printf '%.4d-%.2d-%.2d\n' "${y#0}" "${m#0}&...


1

$ perl -lne 'print reverse split /(-)/, s/\b\d\b/0$&/rg' <<<"19-7-2021" 2021-07-19 Split on dash /(-)/ , with the dashes included in the split up array but before that change the date to double digits in day and/or month.


6

If the format is hardcoded as DD-YY-YYYY, you can also hardcode the whole expression and use substring extraction to reformat, which is very fast: #!/bin/bash dmy=$(date +%d-%m-%Y) ymd="${dmy: -4}-${dmy:3:2}-${dmy:0:2}" echo "$dmy -> $ymd" If the format is D[D]-M[M]-YYYY you could use regular expressions to reformat, which is somewhat ...


11

You could switch to busybox date which allows specifying the input format: $ date=19-7-2021 $ busybox date -D %d-%m-%Y -d "$date" +%F 2021-07-19 (beware it won't accept 019-007-2021 dates for instance). Same with the ast-open implementation of date (unlikely to be available out of the box on your RedHat system): $ date -p %d-%m-%Y -d "$date&...


10

In your case, awk might be a better method: $ awk -F'-' '{printf("%04d-%02d-%02d\n",$3,$2,$1)}' <<< '19-07-2021' 2021-07-19


0

For UTC you can use. date -u --rfc-3339=ns | sed -r 's/ /T/; s/\.([0-9]{3}).*/\.\1Z/'


3

In the terminal: echo $((($(date +%s) + 11644473600) * 10000000)) Detailed explanation: date +%s gets the current time in seconds since 1970. 11644473600 seconds elapsed from 1601 to 1970. 10000000 is the conversion from seconds to 100-nanoseconds.


0

If you are using BSD (or another system that uses the BSD POSIX, like macOS), you can still use date like this: $ date -v+30d -jnf %Y-%m-%d "2015-09-26" +%Y-%m-%d


0

It's rather simple to work out, but here's a variation employing python. #-- cross-platform workaround, see https://unix.stackexchange.com/q/561927/3097 alias stat_mtime='python -c "print(__import__(\"os\").stat(__import__(\"sys\").argv[1]).st_mtime)"' $ stat_mtime /dev/null 1624731195.3483574


4

Answer: no, there is not Because as @Kamil hints at, given an ambiguous date of 01/01/01, there is no way to determine whether this is dd/mm/yy, mm/dd/yy, or yy/mm/dd, etc.


Top 50 recent answers are included