LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2008, 06:30 PM   #1
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Rep: Reputation: 15
Date format lost padding


I have a script that needs to input a start and end date. It works fine until the end of of the month when the start date is say April 30 and the end date is May 1.

My soliton was to check if the second day is smaller then the first, if so, take "1" away from the month. The problem is, I loose the "0" padding on single digit months and I need it to be 2 i.e April needs to be "04", not "4"

I did this little test script up to show what I mean. (If I just use `date +%m` all is fine, if I use "$((`date +%0m`-1))" I loose the padding)



#!/bin/ksh/

clear

echo
echo "Starting day i.e 11"
read date1

echo
echo "Ending day i.e 12"
read date2

echo

if [ $date2 -lt $date1 ]

then

month1="$((`date +%0m`-1))"

else

month1=`date +%m`

fi


echo "The first date is $date1/$month1, and the second date is $date2/`date +%m`."


Any ideas?
 
Old 05-01-2008, 06:50 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by OldGaf View Post
if I use "$((`date +%0m`-1))" I loose the padding
Code:
month1=`date +%0m --date="1 month ago"`
should work, though I don't know why you need explicit +%0m padding, for me +%m shows a leading zero.
 
Old 05-01-2008, 07:25 PM   #3
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn View Post
Code:
month1=`date +%0m --date="1 month ago"`
should work, though I don't know why you need explicit +%0m padding, for me +%m shows a leading zero.


Thanks, that did it!

I added the 0 in an attempt to add the padding..... did not work. But thats all right as your example works fine.....

Thanks again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date format ust Linux - Newbie 2 12-21-2007 01:39 PM
date format monu Linux - Newbie 6 10-08-2007 10:47 AM
Date format Bill Jones Linux - Newbie 1 12-23-2006 11:07 AM
date format kalleanka LQ Suggestions & Feedback 9 11-15-2006 11:36 AM
Regarding date Format ancys Programming 4 08-10-2006 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration