NiteTrip
NiteTrip

Reputation: 198

Validating Date Time In Javascript

I need some help with validating a date time string in Javascript, based on the browser's language.

I can get the datetime format easily enough, for instance if the language is set to pt-BR the format would be

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. I am thinking because Date.parseExact is not able to do times. 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:

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

Upvotes: 7

Views: 126

Answers (2)

Travis Heeter
Travis Heeter

Reputation: 14084

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). I recommend letting VB, or really anything else handle it.

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:

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.

Upvotes: 1

Radonirina Maminiaina
Radonirina Maminiaina

Reputation: 7004

You can use Regex to do this:

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/

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

Updated demo: https://jsfiddle.net/kzzn6ac5/1 or https://regex101.com/r/aT1oL6/1

Further Regex expressions relevant to date matching can be found here.

Upvotes: 1

Related Questions