'How to display date as YYYYMMDD_HH24 using Solaris shell command?

20110216_00

20110216_01

...

20110216_23


20110217_00
..

and so on

I have tried with

date +'%Y%m%d_%H'

but it never starts with 00-23 format but from 01-24 like format, hence I get hour part always incorrect.

Can anybody suggest, how can I get above o/p



Solution 1:[1]

You can do it by manupulating the hour part. Check the snip below.

#/bin/ksh
s=`date +'%Y%m%d_'`
t=` date +'%H'`
let t=$t+1
echo "Required date is " $s$t

It gives

Required date is  20110316_16

Solution 2:[2]

I tried it on SunOS 5.10 This works !

date +%Y%m%d_%H
-> 20130912_02

date +'%Y%m%d_%H'
-> 20130912_02

Can you tell us which Solaris are you using ?

uname -a

Cheers !

Solution 3:[3]

What revision of Solaris are you using? It is roughly 22:30 locally and I see:

mph@sol11express:~$ date +'%Y%m%d_%H'
20110216_22
mph@sol11express:~$ uname -a
SunOS sol11express 5.11 snv_151a i86pc i386 i86pc Solaris
mph@sol11express:~$ echo $SHELL
/bin/bash

which looks to me like it is using 0-23 for hours.

Solution 4:[4]

you can use date -u +'%Y-%m-%d-%H'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sachin Chourasiya
Solution 2 Dipesh Shah
Solution 3 Sean Bright
Solution 4 unixer