The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> UNIX Help
|
How to convert month into numeric ?
Discuss How to convert month into numeric ? in the UNIX Help forum on Dev Shed. How to convert month into numeric ? UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 4th, 2004, 08:09 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 41
Time spent in forums: 3 h 37 m 34 sec
Reputation Power: 10
|
|
|
How to convert month into numeric ?
Hi,
Using UNIX command, Is there any possiblity how to convert "month into numeric ? ...
example :
"Jan" ==> 01
"Feb" ==> 02
Previously, I used CASE statement to convert them, but if you have any better ideas, could I shared with you ?
|

March 4th, 2004, 03:44 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
It's real easy in perl -- all you have to do is create a hash object. Are you allowed to use perl or not?
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

March 5th, 2004, 02:14 AM
|
 |
perl virgin
|
|
Join Date: Oct 2003
Location: Seattle, WA
Posts: 455
  
Time spent in forums: 2 Days 14 h 18 m
Reputation Power: 13
|
|
|
please....perl is too much for this thing...
just do date +%m
%m will display month in numbers
|

March 6th, 2004, 05:14 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|
@papajohns:
How do you solve it with a month different from the current one then? I think the original posting implied that he wants to find it for any month.
M.
|

March 6th, 2004, 05:28 AM
|
 |
perl virgin
|
|
Join Date: Oct 2003
Location: Seattle, WA
Posts: 455
  
Time spent in forums: 2 Days 14 h 18 m
Reputation Power: 13
|
|
|
ah..ic...... if its programin he wants..then i guess a hash is good thing..although a simple shell script can do it too...just set variables...
jan=1, feb=2, so on....but from its context i understand it as he wants it to change month to a number at the unix command line
|

March 6th, 2004, 08:08 AM
|
 |
funky munky
|
|
Join Date: Jul 2001
Location: UK
Posts: 1,446
  
Time spent in forums: 2 Days 18 h 45 m 36 sec
Reputation Power: 14
|
|
Here's a thought:
Code:
#!/bin/sh
set `echo "Jan Feb Mar Apr May Jun Jul Aug Sep Nov Dec"`
echo $3
This will output 'Mar'. Perhaps you could use this somehow...?
|

May 22nd, 2004, 09:31 AM
|
|
mcq1
|
|
Join Date: May 2004
Location: GA
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
convert month to numeric
#!/bin/ksh
##---- function to convert 3 char month into numeric value ----------
convertDate() {
echo $1 | awk -F"-" '{print $1,$2,$3}' | read day mm yyyy ## split the date arg
typeset -u mmm=`echo $mm` ## set month to uppercase
typeset -u months=`cal $yyyy | grep "[A-Z][a-z][a-z]"` ## uppercase list of all months
i=1 ## starting month
for mon in $months; do ## loop thru month list
## if months match, set numeric month (add zero if needed); else increment month counter
[[ "$mon" = "$mmm" ]] && typeset -xZ2 monthNum=$i || (( i += 1 ))
done ## end loop
echo $day$monthNum`echo $yyyy | cut -c3-` ## return all numeric date format ddmmyyyy
}
##---- main ---------------------------------------------------------
convertDate 12-May-2004 ## function call with date argument
|

December 18th, 2012, 01:44 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 1
Time spent in forums: 4 m 35 sec
Reputation Power: 0
|
|
|
Converting month name to Month number
try this out:
file_month=`ls -ltr <file_name> | awk '{print $6}' | sed 1q`
month_map="Jan 01\nFeb 02\nMar 03\nApr 04\nMay 05\nJun 06\nJul 07\nAug 08\nSep 09\nOct 10\nNov 11\nDec 12"
month_number=`echo $monthmap | grep $filemonth | awk '{print $2}' | sed 1q`
echo $month_number
|

December 18th, 2012, 02:19 PM
|
 |
Providing fuel for space ships
|
|
Join Date: Mar 2004
Location: nr Edinburgh, Scotland
|
|
Last post was over 8.5 YEARS ago. Talk about thread necromancy . . . . . 
__________________
The No Ma'am commandments:
1.) It is O.K. to call hooters 'knockers' and sometimes snack trays
2.) It is wrong to be French
3.) It is O.K. to put all bad people in a giant meat grinder
4.) Lawyers, see rule 3
5.) It is O.K. to drive a gas guzzler if it helps you get babes
6.) Everyone should car pool but me
7.) Bring back the word 'stewardesses'
8.) Synchronized swimming is not a sport
9.) Mud wrestling is a sport
|

March 26th, 2013, 04:15 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 1
Time spent in forums: 7 m 26 sec
Reputation Power: 0
|
|
|
month to integer
Two lines,
$month=date("m", time()); //returns string "03" for march
$month=intval($month); //makes it integer 3
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|