Javascript:将来以日期/日期/时间格式查看日期? [英] Javascript: check date in future in dd/mm/yyyy format?

查看:121
本文介绍了Javascript:将来以日期/日期/时间格式查看日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查用户输入字段以查看是否在将来,如果它是dd / mm / yyyy格式,但我不知道为什么我的代码的格式部分不会触发!实际上没有什么似乎在Jsfiddle上工作,但至少我的未来检查日期功能在本地工作。

I'm trying to check the users input field to see if it is in the future and if it is in dd/mm/yyyy format but I have no idea why the format part of my code doesn't fire at all! In fact nothing seems to be working on Jsfiddle but at least my "check date in the future" function works locally.

我不知道正确的方法这个。

I don't know the correct way of going about this.

解释一下,我创建了这个 FIDDLE

to explain this, I've created this FIDDLE

这是我完整的JavaScript代码。我需要保持纯粹的JavaScript的方式:

And this is my full javascript code. I need to stay with pure javascript by the way:

function checkdate(){
    //var sendDate = document.getElementById('send_year').value + '/' + document.getElementById('send_month').value + '/' + document.getElementById('send_day').value;
    var sendDate = document.getElementById('returning_date').value;






    sendDate = new Date(Date.parse(sendDate.replace(/-/g,' ')))
    today = new Date();
    today.setHours(0,0,0,0)
    if (sendDate < today) {
        //alert('The  date can\'t be in the past. Please pick another date.');

        document.getElementById('error8').innerHTML = 'The  date can\'t be in the past. Please pick another date.';
        return false;
    }
    else
{
   document.getElementById('error8').innerHTML = '';
}


if(sendDate.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/))
{
  alert('works out');
}

}

有人可以就此问题提供建议

could someone please advise on this issue?

提前感谢

推荐答案

日期字符串应始终为手动解析,你不应该允许Date构造函数或者Date.parse来解析字符串(Date构造函数以与Date.parse完全相同的方式解析字符串)。

Date strings should always be manually parsed, you should never allow the Date constructor or Date.parse to parse strings (the Date constructor parses strings in exactly the same way Date.parse does).

解析和验证日期字符串是相当简单的,只需解析字符串并查看是否得到有效的日期:

To parse and validate a date string is fairly straight forward, just parse the string and see if you get a valid date:

/* Parse a string in d/m/y format. Separator can be any non–digit
** Avoid conversion of two digit dates to 20th century
** Returns an invalid Date if string is not a valid date (per ECMA-262)
**
** @param {string} s - Date string to parse
** @returns {Date}
*/
function parseDMY(s) {
  var b = s.split(/\D/);
  var d = new Date();
  d.setHours(0,0,0,0);
  d.setFullYear(b[2], --b[1], b[0]);
  return d && d.getMonth() == b[1]? d : new Date(NaN);
}

// Test valid date
document.write(parseDMY('23/01/2016'));
// Test invalid date
document.write('<br>' + parseDMY('35/12/2016'));

请注意,这将接受日期如1/5/16,对待为1月1日,0016.如果要保证日期和月份值有两位数字和年份,则添加:

Note that this will accept a date like 1/5/16 and treat is as 1 May, 0016. If you want to guarantee that the day and month values have two digits and the year for, then add:

/^\d\d\D\d\d\D\d{4}$/.test(s)

到验证测试结束。不过,我不喜欢强迫2位数字的日子,因为人们通常不会将日期写成01/08/2016,他们使用1/8/2016。

to the validation test at the end. However, I don't like forcing 2 digits for day and month as people don't usually write dates as "01/08/2016", they use "1/8/2016".

这篇关于Javascript:将来以日期/日期/时间格式查看日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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