Skip to content Skip to sidebar Skip to footer

Format Yyymmdd Date

I need this javascript function to not split a yyyy-mm-dd but split a yyymmdd. Current date formats are 2011-11-02 but this function needs to work with 20111102. Script: fu

Solution 1:

You could do this:

var date = date.match(/(\d{4})(\d{2})(\d{2})/); // [yyyymmdd, yyyy, mm, dd]
date.shift(); // remove yyyymmdd from array.
...

Solution 2:

Continuing my work from your other question, I came up with the following function to solve your problem, using substr:

functionevaluateEventDate(eventdate, date){
    var event_y = eventdate.substr(0, 4);
    var event_m = eventdate.substr(4, 2);
    var event_d = eventdate.substr(6, 2);

    var date = date.split('-');

    if (event_y === 'yyyy')
    {
        event_y = date[0];
    }

    if (event_m === 'mm') 
    {
        event_m = date[1];
    }

    if (event_d === 'dd')
    {
        event_d = date[2];
    }

    return event_y + '-' + event_m + '-' + event_d;
};

Post a Comment for "Format Yyymmdd Date"