简体   繁体   English

在Javascript中验证日期时间

[英]Validating Date Time In Javascript

I need some help with validating a date time string in Javascript, based on the browser's language. 我需要一些帮助,根据浏览器的语言验证Javascript中的日期时间字符串。

I can get the datetime format easily enough, for instance if the language is set to pt-BR the format would be 我可以很容易地获得日期时间格式,例如,如果语言设置为pt-BR格式将是

dd/MM/yyyy HH:mm:ss

I tried using something like this: 我试过用这样的东西:

var dateFormat = "dd/MM/yyyy HH:mm:ss";
var x = Date.parseExact($("#theDate").val(), dateFormat);

However x is always Null. 但是x总是空的。 I am thinking because Date.parseExact is not able to do times. 我在想因为Date.parseExact无法做到。 I need to be able to do this for all browser languages and I would prefer to not use another library. 我需要能够为所有浏览器语言执行此操作,我宁愿不使用其他库。 Using Regex is out also since I would need to write so many different expressions. 使用正则表达式也是因为我需要编写这么多不同的表达式。

Does anyone have any suggestions to help me ge on the right track? 有没有人有任何建议可以帮助我走上正轨? I am also not against using a webmethod. 我也不反对使用网络方法。

I have tried using the following webmethod, which works with en-US but nothing else: 我尝试使用以下webmethod,它与en-US一起使用,但没有别的:

Public Function ValidateDates(ByVal strDate_In As String) As String
    Dim theFormat As String = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern() + " " + CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern()
    Try
        Dim d As DateTime = DateTime.ParseExact(strDate_In, theFormat, CultureInfo.CurrentCulture)
        Return "true"
    Catch ex As Exception
       Return "false"
    End Try
End Function

You can use Regex to do this: 您可以使用Regex执行此操作:

var dateFormat = "dd/MM/yyyy HH:mm:ss";
var x = $("#theDate").val().match(/^(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2})$/);
console.log(x);

Demo: https://jsfiddle.net/kzzn6ac5/ 演示: https//jsfiddle.net/kzzn6ac5/

update The following regex may help you and improve it according to your need: 更新以下正则表达式可以帮助您并根据您的需要进行改进:

^((\d{2}|\d{4})[\/|\.|-](\d{2})[\/|\.|-](\d{4}|\d{2}) (\d{2}):(\d{2}):(\d{2}))$

It matches the following format with /.- and yyyy/mm/dd hh:mm:ss or dd/mm/yyyy hh:mm:ss 它符合以下格式: /.-yyyy/mm/dd hh:mm:ssdd/mm/yyyy hh:mm:ss

Updated demo: https://jsfiddle.net/kzzn6ac5/1 or https://regex101.com/r/aT1oL6/1 更新的演示: https//jsfiddle.net/kzzn6ac5/1https://regex101.com/r/aT1oL6/1

Further Regex expressions relevant to date matching can be found here . 可以在此处找到与日期匹配相关的其他正则表达式。

JavaScript date objects are deceptively easy, I worked with them in a project and they had a sneaky learning-curve that takes a lot of time to master (as opposed to the rest of JavaScript, which is relative child's play). JavaScript日期对象看起来很容易,我在一个项目中与他们一起工作,他们有一个偷偷摸摸的学习曲线需要花费大量时间来掌握(而不是其他JavaScript,这是相对孩子的游戏)。 I recommend letting VB, or really anything else handle it. 我建议让VB,或其他任何其他处理它。

But if you want a way to do it in javascript, without Regex (as stated in your question), you could perform string operations on it like this: 但是如果你想在javascript中实现它,没有正则表达式(如你的问题中所述),你可以像这样对它执行字符串操作:

try {
    var user_input = $("#theDate").val();
    var split = user_input.split(" "); // 0: date, 1: time
    var split_time = split[1].split(":"); // 0: hours, 1: minutes, 2: seconds

    d.setHours(split_time[0]);
    d.setMinutes(split_time[1]);
} catch {
    // not in a valid format
}

This solution assumes the input is in the correct format, and if an error occurs, it's not. 此解决方案假设输入格式正确,如果发生错误,则不是。 It's not the best way of doing things, but JS Date objects are seriously horrible. 这不是最好的做事方式,但JS Date对象非常可怕。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM