#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    41
    Rep Power
    12

    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 ?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. perl virgin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Seattle, WA
    Posts
    457
    Rep Power
    14
    please....perl is too much for this thing...

    just do date +%m

    %m will display month in numbers
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    @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.
  8. #5
  9. perl virgin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Seattle, WA
    Posts
    457
    Rep Power
    14
    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
  10. #6
  11. funky munky
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    1,446
    Rep Power
    16
    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...?
  12. #7
  13. No Profile Picture
    mcq1
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    GA
    Posts
    7
    Rep 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
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep 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
  16. #9
  17. Providing fuel for space ships
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2004
    Location
    nr Edinburgh, Scotland
    Posts
    14,382
    Rep Power
    3848
    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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    month to integer


    Two lines,

    $month=date("m", time()); //returns string "03" for march
    $month=intval($month); //makes it integer 3

IMN logo majestic logo threadwatch logo seochat tools logo