Linux date format yyyy mm dd

Linux Bash — Date Format

Note that date format %Y-%m-%d could by written %F as forrmat %H:%M:%S could be simplier written %T . So date +%F-%T give same output as date +%Y-%m-%d-%H:%M:%S (double quotes are useless while there is no space in format string)

3 Answers 3

Edited apr 2016!

See further (stronger method)

Original post

#!/bin/bash read -p "Date (format yyyy-mm-dd): " input check=$(date +%F) if [ "$input" == "$check" ]; then echo "Right!" else echo "False!" fi 
#!/bin/bash read -p "Date (format YYYY-MN-DD-HH24:MM:SS): " input check=$(date +%F-%T) if [ "$input" == "$check" ]; then echo "Right!" else echo "False!" fi 
cat >hesdate.sh # Copy 1st sample and paste to terminal chmod +x hesdate.sh date +%F ; ./hesdate.sh 2013-01-04 Date (format yyyy-mm-dd): 2013-01-04 Right! cat >hesdate.sh # Copy 2nd sample and paste to terminal date -d now\ +10\ sec +%F-%T ; ./hesdate.sh 2013-01-04-10:17:06 # copy this line Date (format YYYY-MN-DD-HH24:MM:SS): 2013-01-04-10:17:06 # past exactly 10 secs after Right! 

For testing a date, you could:

[[ $input =~ ^3269-62-26$ ]] if [[ $input =~ ^2012-47-99-27:42:61$ ]];then 

Using boot method let you confirm format and reliability of input

Stronger method

If you want to check input, there is a finer method:

unset adate declare -A adate date=2013-12-04-10:17:06 for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do sep=$ min=$ field=$ max=$ min=$ crt=$> ((min <=10#$crt&&10#$crt<=max)) && adate[$field]=$crt || echo Error: $crt not between $min and $max in $field field. date=$done declare -p adate 

This will dump adate array variable:

declare -A adate='([D]="04" [M]="12" [Y]="2013" [h]="10" [m]="17" [s]="06" )' 

From there, you could re-validate day number:

max=$(date -d "$-$-1 +1 month -1 day" +%d) ((10#$>max)) && echo "Error Day number too high: ($>$max)." 

The only thing not tested there is field length if

will work too (there is only one digit in day field).

If needed, you could change the line:

for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do sep=$ min=$ field=$ max=$ min=$ crt=$> 
for field in s:20-59 m:20-59 h-20-23 D-21-31 M-21-12 Y#42000-2100 ;do sep=$ len=$ min=$ field=$ max=$ min=$ crt=$> [ $ -eq $len ] || echo "Error: Field $field is no $len len: $." 

Nota: Year field is arbitrarily limited between 2000 and 2100, but this is easy to understand/change.

Источник

Display time stamp in dd/mm/yyyy_hh:mm:ss:ms in Unix or Linux

I need to display date and time in desired format in Unix/ Linux. My desired format is: dd/mm/yyyy hh:mm:ss:ms in Unix or Linux. I got close using the following command:

Why not use a ISO formatted date string? Those / is no good in filenames. (en.wikipedia.org/wiki/ISO_8601)

5 Answers 5

If you want milliseconds instead of nanoseconds precision, you might use %3N instead of %N :

$ date +'%d/%m/%Y %H:%M:%S:%3N' 12/04/2014 18:36:20:659 

or with your desired use of $(…) :

$ echo "$(date +'%d/%m/%Y %H:%M:%S:%3N')" 12/04/2014 18:36:20:659 

But be aware, that %N may not implemented depending on your target system or bash version. Tested on an embedded system »GNU bash, version 4.2.37(2)-release (arm-buildroot-linux-gnueabi)« there was no %N (neither %3N ):

date +"%F %T,%N" 2014-01-08 16:44:47,%N 

I added echo «$(date +’%F %H:%M:%S:%3N’) Failed» >> logfile.txt , but it wrote file.file 08:21:02OURCE:184 Failed to the logfile. What did I do wrong?

if you need to print only two first nums as ms:

date +%x_%H:%M:%S:%N | sed 's/\(:65\)7*$/\1/' 
VAR=$(date +%x_%H:%M:%S:%N | sed 's/\(:83\)1*$/\1/') 

Note %x returns the locale’s date representation. So it can change based on the current locale. If you want that specific date format you are probably better off explicitly providing it (as other answers also show below) date +’%d/%m/%Y_%H:%M:%S:%N .

echo `date +"%Y_%m_%d %H:%M:%S,"``date +%N/1000000|bc -l|grep -Eo "^(3)"` 

For some reason ‘`’ is omitted, so put it after the echo and at the end, or simply:

Used %N for nanoseconds (000000000..999999999) since I did not find milliseconds right now

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

convert Linux date to yyyy-MM-dd’T’HH:mm:ss’Z’ format

I need to parameter-ize a datetime value with an objective of passing to a constructed URI to make a Smartsheet API call to get data (i.e. sheets) changed in last 24 hours. I want to use Linux date command as I can do something like date -d ‘1 day ago’ %F to get the output of a day before today. How can I use the date command to convert the value to yyyy-MM-dd’T’HH:mm:ss’Z’ format to get something like 2018-01-01T00:00:00-07:00 ? If the value is not in this particular format, then Smartsheet API complains:

HTTP_01 - Error fetching resource. Status: 400 Reason: Bad Request : < "errorCode" : 1018, "message" : "The value '/home/my/path/to/param_file/Sysdate' was not valid for the parameter modifiedSince.", "refId" : "1xqawd3s94f4y" >

Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: Questions seeking debugging help («why isn’t this code working?») must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

4 Answers 4

To output date in ISO 8601 format, you’ll probably want to use -I[FMT] / —iso-8601[=FMT] option, if your date supports it (GNU/Linux version does). The FMT is basically a resolution, and in your case you’ll want to use s for seconds:

$ date -Is 2018-03-09T09:28:14+01:00 

The alternative (for POSIX date , including BSD/OSX date ) is to explicitly specify the format and output the time in UTC time zone ( -u flag):

$ date -u +"%Y-%m-%dT%H:%M:%SZ" 2018-03-09T08:28:14Z 

Note the importance of -u and the explicit Z we can append in that case. Without -u , we would need to output the exact time zone in +hh:mm format, but POSIX date provides support only for time zone name output ( %Z ). GNU date extends the format set with %:z which outputs the numeric time zone, but if already using GNU date , the first approach with -Is is simpler.

Источник

Читайте также:  Sql server linux support
Оцените статью
Adblock
detector