1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Split Date (month, day, year) using Javascript

Discussion in 'JavaScript' started by KingCobra, Sep 2, 2011.

  1. #1
    Firstly thanks to DigitalPoint and its users for providing me many help and solution previously.

    I have a new problem. I have the following form that have a date field with date vale.

    <form name="signup" onsubmit="return Valid(this);">
    <input type="text" name="date" value="09/22/2011">
    <input type="submit">
    </form>

    I need a JavaScript that will take the date value on form submit and split it to 3 parts (such as month, day, year) and then return.
    So that I can insert separately these 3 parts in my MySql table (3 fields) during the form submit.

    Like bellow:
    Month Day Year
    ------ ----- ------
    09 22 2011
     
    KingCobra, Sep 2, 2011 IP
  2. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #2
    This should be done by server side... But if you need this functionallity in javascript, you can use:

    
    var foo = "09/22/2011";
    var arr = foo.split("/");
    alert(arr[0])
    alert(arr[1])
    alert(arr[2])
    
    Code (markup):
     
    Jan Novak, Sep 2, 2011 IP
  3. AdWorkMedia

    AdWorkMedia Member

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    28
    #3
    Yea, I think using PHP would be best for this, but if you are using a JS calendar or any other type of JS to handle it then you will want to use the .split function like in the example above.
     
    AdWorkMedia, Sep 2, 2011 IP
  4. salimali

    salimali Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    I have some code like below, What is wrong on it ? I am getting error.

    onClick: function () {
    $.ajax({
    type: 'POST',
    url: 'http://www.mysite.com/data.php',
    dataType: 'json',
    success: function (data) {



    var res = [];
    for (var i in data) {
    var rows = data;


    res.push({
    event_id: rows[0],
    event_name: rows[1],
    date: rows[2],
    venue:rows[3],
    url:rows[4],

    });

    var eventdate = $data.date;
    var splitdate = eventdate.split("-");
    year = splitdate[0],
    month = splitdate[1],
    day = splitdate[2]
    if (month == 01) {
    month = "Jan"
    }
    if (month == 02) {
    month = "Feb"
    }

    if (month == 03) {
    month = "Mar"
    }

    var newDate = year - month - day;
    }
    viewModel.dSource(res);
    }
    });
    }
     
    salimali, Sep 17, 2013 IP