Date format in Bash Script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Date format in Bash Script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-02-2013
parpaa parpaa is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 15 November 2013, 10:31 AM EST
Posts: 32
Thanks: 12
Thanked 1 Time in 1 Post
Date format in Bash Script

Hi Experts,

We get "Day" of a month in a variable, so how to make date of out it?

To make more sense
if my variable $DAY contains "12" and month and year will be current date (as of today)
so I want to see as output as 2013-09-12.

How can I achive this bash script??

Any help is highly appreciated.

Thank you
Sponsored Links
    #2  
Old 09-02-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 13 December 2014, 6:28 PM EST
Location: ZŁrich
Posts: 8,489
Thanks: 333
Thanked 1,050 Times in 916 Posts
Simply, using:

Code:
# DAY=12
# date +%Y-%m-$DAY
2013-09-12

There's no validation, though:

Code:
# DAY=31          
# date +%Y-%m-$DAY
2013-09-31

The Following User Says Thank You to Scott For This Useful Post:
parpaa (09-02-2013)
Sponsored Links
    #3  
Old 09-02-2013
Skrynesaver's Avatar
Skrynesaver Skrynesaver is offline Forum Advisor  
Grumpy old geek
 
Join Date: Mar 2011
Last Activity: 17 December 2014, 9:23 AM EST
Location: …ire
Posts: 830
Thanks: 20
Thanked 187 Times in 183 Posts

Code:
$ day=02
$ date +%Y-%m-$day
2013-09-02

    #4  
Old 09-02-2013
parpaa parpaa is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 15 November 2013, 10:31 AM EST
Posts: 32
Thanks: 12
Thanked 1 Time in 1 Post
Thank youuuuuu

Thank you Scott
Sponsored Links
    #5  
Old 09-02-2013
Skrynesaver's Avatar
Skrynesaver Skrynesaver is offline Forum Advisor  
Grumpy old geek
 
Join Date: Mar 2011
Last Activity: 17 December 2014, 9:23 AM EST
Location: …ire
Posts: 830
Thanks: 20
Thanked 187 Times in 183 Posts
Quote:
Originally Posted by Scott View Post
Simply, using:

Code:
# DAY=12
# date +%Y-%m-$DAY
2013-09-12

Checking validity of scripts as root, there's confidence for ya
The Following User Says Thank You to Skrynesaver For This Useful Post:
Scott (09-02-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to format string/date in bash? ford99 Shell Programming and Scripting 2 07-26-2011 01:24 PM
How to increment a user defined date value in the DATE format itself using shell script? sunil087 Shell Programming and Scripting 1 12-22-2009 06:29 AM
Changing from Excel date format to MySQL date format figaro UNIX for Dummies Questions & Answers 2 08-11-2009 04:23 PM
convert date format to mysql date format in log file hazno Shell Programming and Scripting 3 03-03-2009 09:05 AM
bash date format vadharah UNIX for Dummies Questions & Answers 1 04-01-2008 02:37 PM



All times are GMT -4. The time now is 10:22 PM.