2017-09-25 38 views
0

我有三种格式存储在数据库设置表中。动态计算基于日期格式的年龄

<select> 
    <option value="d-m-Y">dd/mm/yyyy</option> 
    <option value="Y-m-d">yyyy/mm/dd</option> 
    <option value="m-d-Y">mm/dd/yyyy</option> 
</select> 

我能够获得该行

var dateFormat = getDateFormat(); //of the below code. 

function GetAge(dateString) 
{ 
    var today = new Date(); 
    var dateFormat = getDateFormat(); 
    var birthDate = new Date(dateString); 

    var age = today.getFullYear() - birthDate.getFullYear(); 
    var m = today.getMonth() - birthDate.getMonth(); 
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) 
    { 
    age--; 
    } 
    return age; 
} 

现在我想它应该检查格式先再计算年龄的格式值像d-m-YY-m-dm-d-Y ... 请帮助我 !!!

+0

那么究竟是什么问题与您当前的代码? – Rajesh

+0

你在哪里使用'dateFormat'? – Durga

+0

向我们展示了一个现场演示 – Pedram

回答

0

您正在使用jquery ui,所以在计算之前使用datepicker.formatDate这样。

var dateFormat = getDateFormat();// Return pattern yy-mm-dd 
    var today =$.datepicker.formatDate(dateFormat , new Date());; 
    var birthDate = $.datepicker.formatDate(dateFormat , new Date(dateString)); 
    //Your age calculation logic