日期格式为dd / MM / yyyy格式,但javascript将其视为MM / dd / yyyy [英] Date format in dd/MM/yyyy format but javascript take it as MM/dd/yyyy

查看:84
本文介绍了日期格式为dd / MM / yyyy格式,但javascript将其视为MM / dd / yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以dd / MM / yyyy格式在文本框中输入日期

下面给出了计算年龄的函数。

输入日期为12/02/2000时是2000年2月12日这个函数计算年龄。

但是当这一天是26/02/2000或者日> 12时,不工作。

函数calculateAge(elt){

bdate = new Date(elt.value);

birthMonth = bdate.getMonth();

birthDay = bdate.getDate();

birthYear = bdate.getFullYear();

todayDate = new Date();

todayYear = todayDate.getFullYear();

todayMonth = todayDate.getMonth();

todayDay = todayDate.getDate();

age = todayYear - birthYear;

alert(birthMonth);

if(todayMonth< birthMonth - 1){

age--;

}



if(b irthMonth - 1 == todayMonth && todayDay< birthDay){

age--;

}

返回年龄;

}

When enter date in textbox in dd/MM/yyyy format
the function for calculate age is given below.
when enter date as 12/02/2000 which is 12 feb 2000 this function calculate age.
but when the day is 26/02/2000 or day>12,not work.
function calculateAge(elt) {
bdate = new Date(elt.value);
birthMonth = bdate.getMonth();
birthDay = bdate.getDate();
birthYear = bdate.getFullYear();
todayDate = new Date();
todayYear = todayDate.getFullYear();
todayMonth = todayDate.getMonth();
todayDay = todayDate.getDate();
age = todayYear - birthYear;
alert(birthMonth);
if (todayMonth < birthMonth - 1) {
age--;
}

if (birthMonth - 1 == todayMonth && todayDay < birthDay) {
age--;
}
return age;
}

推荐答案

您好b $ b

使用此javascript方法将文本框中的输入转换为有效的日期对象





Hi
use this javascript method to convert the input from textbox to a valid Date Object


bdate = GetDateObject (elt.value);







var GetDateObject = function ( dateString){
           
            var array = dateString.split('/');

            var day = parseInt(array[0]);
            var month = parseInt(array[1]);
            var year = parseInt(array[2]);
            var dateObject = new Date(year, month - 1, day);
            return dateObject;
             }


这篇关于日期格式为dd / MM / yyyy格式,但javascript将其视为MM / dd / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆