简体   繁体   English

基于Java用户输入,设置Date()对象的正确方法是什么

[英]what is the correct way to set a Date() object based on user input in Javascript

I'm making an age verification modal and I'm a little stuck. 我正在制作年龄验证模式,但有点卡住了。

On lines 4-7 I create a javascript date object and try to fill it with the month, date and year, but it has become apparent to me that each of the lines 5-7 is overwritting the previous one. 在第4-7行中,我创建了一个javascript日期对象,并尝试用月,日和年填充它,但是对我来说很明显,第5-7行中的每一个都覆盖了前一个。

$('#tier3').change(function(){
    var tier1 = $('#tier1').find(":selected").text();
     var tier2 = $('#tier2').find(":selected").text();
     var tier3 = $('#tier3').find(":selected").text();
     var userDOB = new Date();
     userDOB.setMonth(tier1);
     userDOB.setDate(tier2);
     userDOB.setFullYear(tier3);
     var now = Date.now();
     userAge = now - userDOB;
     if( userAge >= 588591547188){
        set_popup_cookie();
        popup_wrapper.style.visibility="hidden";
     }else {
        /*  window.location = "http://www.smokefree.gov/"; */
     }
});

I also tried substituting lines 4-7 with 我也尝试用4-7行代替

var userDOB = Date.parse(tier . " " . tier2 . ", " . tier3 );

But I got a syntax error 但是我遇到语法错误

you can use Date constructor new Date(year, month, day [, hour, minute, second, millisecond]); 您可以使用Date构造函数new Date(year, month, day [, hour, minute, second, millisecond]);

as below 如下

var userDOB = new Date(tier3, tier2, tier1);
var now = Date.now();
userAge = now - userDOB;
 if( userAge >= 588591547188){
    set_popup_cookie();
    popup_wrapper.style.visibility="hidden";
 }else {
    /*  window.location = "http://www.smokefree.gov/"; */
 }

check tier3, tier2, tier1 having expected values before calculating age 在计算年龄之前检查tier3, tier2, tier1具有期望值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript日期对象基于什么 - Javascript date object based on what JavaScript根据用户输入设置计时器 - JavaScript set a timer based on user input 用html(来自用户)输入日期和时间并用Javascript接收日期和时间的最佳方法是什么? - What is the best way to have date and time input in html (from user) and receive in Javascript? 反应-用用户输入进行API调用的正确方法是什么? - React - What is the correct way to make an API call with user input? 在javascript中测试对象是否是jQuery对象的正确/正确方法是什么? - What's the correct/proper way to test if an object is a jQuery object in javascript? 根据用户是否登录在 React 中更改 Navbar 值的正确方法是什么? - What is the correct way to change Navbar values in React based on if user is logged in? 默认javascript日期返回对象的正确格式是什么 - What's the correct format of the default javascript Date returned object JavaScript:使用名称和默认值设置对象参数的正确方法 - JavaScript: correct way to set object parameters with name and default value 使用 CSS 和 Javascript 向 HTML DOM 对象添加动画的正确方法是什么? - What is the correct way of adding animation to an HTML DOM object with CSS and Javascript? 使用JavaScript对DOM对象执行算术运算的正确方法是什么? - What is the correct way to use JavaScript to perform arithmetic on a DOM object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM