Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts

    snippet: convert months to numbers (when you fetch it from DB or something)

    Code:
    function assignmonth()
    {
    	g = (e==" January ")? 1 : (e==" February ")? 2 : (e==" March ")? 3 : (e==" April ")? 4 : (e== " May ")? 5 : (e== " June ")? 6 : (e==" July ")? 7: (e==" August ")? 8 : (e==" September ")? 9 : (e==" October ")? 10 : (e==" November ")? 11 : (e==" December ")? 12 : false ;
    }
    Note: Take care of the spaces for example, i have posted (e==" January ") not (e=="January"), and g will have value 'false' or 'NaN' when neither of the cases is true, which could mean there is some error
    Last edited by Peeyush; 05-14-2012 at 04:08 PM.

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Why not something like this which returns false if nothing found so you are able to use it in an if statement and be able to check if a month was found.

    Code:
    (function ($) {
        var Months = {"january": 1, "febuary": 2, "march": 3, "april": 4, "may": 5,
                      "june": 6, "july": 7, "august": 8, "september": 9,
                      "october": 10, "november": 11, "december": 12};
        $.assignMonth = function (month) {
            return ((month = Months[month.toLowerCase().replace(/\s/g, "")])) ? month : false;
        };
    }(window));
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    And it should definitely return 0 for January, 1 for February and so on because internally this is how Javascript date formats will represent the months anyway.

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Who says that Javascript will be running the output through the date function?

    All the OP was doing is outputting the numeric value for the corresponding month in humanly readable counts (e.g January = 1 not 0).

    If however you want to get the Javascript internal value then this will do it.

    Code:
    (function ($) {
        var Months = {"january": 1, "febuary": 2, "march": 3, "april": 4, "may": 5,
                      "june": 6, "july": 7, "august": 8, "september": 9,
                      "october": 10, "november": 11, "december": 12};
        $.assignMonth = function (month, internal) {
            return ((month = Months[month.toLowerCase().replace(/\s/g, "")])) ?
                   (internal && internal === true ? month - 1 : month) : false;
        };
    }(window));
    Code:
    // Standard
    assignMonth("  January  ");  // 1
    
    // Internal
    assignMonth("  January  ", true);  // 0
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I was trying to assign numbers to months, first did it in if, else if format, then i remembered about this conditional operator so i posted it her thinking it might be useful to others, and i hope it is. am gonna post a few more soon

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    That is good however I still say instead of 13 it should be false, Meaning nothing found.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #7
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts
    thanks btw posted a new snippet here : http://www.codingforums.com/showthre...38#post1228438


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •