Register
It is currently Sun Sep 21, 2014 12:04 am

Date conversion


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sat Oct 15, 2011 9:09 am   

Joined: Sat Oct 15, 2011 8:55 am
Posts: 2
I'm trying to enter a date in the format 15/10/11 and have my script return 151011 for me but it's not playing ball

Code:
#!/bin/bash
    echo 'Please Enter the Date you want to restore from in DD/MM/YY format'
    read restoredate
    date --utc --date "$restoredate" +%d%m%y



It keeps giving me an invalid day as it thinks 15 is the month of the year. I've messed around with %d%m but still get the same. Anything obvious I'm missing?


Top
 Profile  
 PostPosted: Sat Oct 15, 2011 10:07 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
Hi,

one way could be to switch DD and MM, like this:
Code:
$ read restore_date
15/10/11   
$ restore_date=$(IFS='/'; set -- $restore_date; echo $2/$1/$3)
$ date -u -d "$restore_date" +%d%m%y
151011
or:
Code:
$ read restore_date
15/10/11
$ IFS='/' read dd mm yy <<<"$restore_date"
$ date -u -d "$mm/$dd/$yy" +%d%m%y
151011


Top
 Profile  
 PostPosted: Mon Oct 17, 2011 12:38 pm   

Joined: Sat Oct 15, 2011 8:55 am
Posts: 2
Brilliant, thanks a bunch


Top
 Profile  
 PostPosted: Sat Nov 05, 2011 5:59 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Since you're using bash(1), you can even do that:

Code:
#!/bin/bash

read -rp 'Please Enter the Date you want to restore from in DD/MM/YY format: '
printf '%s\n' "${REPLY//\/}"


It simply prints the string entered without slashes.
You don't need to use an external program, i.e. date(1).

Also, you should add some conditions to check if the date is valid.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP