0

I have a table of data that needs to be sorted and in order to do so, I have to determine the datatype of the columns: numeric, text, or date.

And no, I never know what the datatype of column will be and it will always have different data.

I have to extrapolate the datatype by looking at the textcontent of the cell.

The one I'm having an issue with is the date.

new Date("MOD-001-01") actually returns a real date of 1/1/2001.

new Date("This is not a date 1") also returns a real date, again 1/1/2001.

Is there any way to reliably test a value to see if its a date?

Andres Gardiol
  • 1,312
  • 15
  • 22
Eric Cooper
  • 121
  • 1
  • 7
  • 3
    What type of format do you need to support? – Unmitigated Jul 11 '23 at 14:49
  • 3
    Can you explain _why_ you have to guess the data type? Because just saying that "you never know what the datatype will be" is not an explanation of the context in which you're currently trying to program a solution. If your data source is _any_ sort of database, you know the column types. And if you don't know what your data source is, what on earth kind of program are you writing? – Mike 'Pomax' Kamermans Jul 11 '23 at 14:50
  • 1
    Check https://stackoverflow.com/questions/7445328/check-if-a-string-is-a-date-value – James Jul 11 '23 at 14:55
  • The tables of data are derived from an SQL query. The user can create any query they want so I never know what the data is they are returning. On the screen I have the ability to click a column header to sort the table by that column. The data in the column might be numeric, text, or date. Each sorts differently, hence the need to determine what type of data it is in order to properly sort. – Eric Cooper Jul 11 '23 at 14:56
  • 1
    A 100% fool proof solution might be impossible without knowing the date format. 23-11-03 could be a date but could also be a serial number? Any solution would be based on a best effort. Using a libary like moment that james linked might be your best bet if you can't work towards a more robuust solution. – Mark Baijens Jul 11 '23 at 14:58
  • Thanks for the suggestion for Moment.js. To add even more of a problem the formats might be different based on the region the user is in. – Eric Cooper Jul 11 '23 at 15:08
  • @EricCooper If the unpredictable arrangement of data is derived from a SQL query, you *should* be receiving your dates in a consistent format, or at worst, a very small set of formats, perhaps one for DATE and another for TIMESTAMP. If you try to discern every possible date, you're likely to encounter false positives in your `text` column. – phatfingers Jul 11 '23 at 16:24
  • This topic has been [done to death](https://stackoverflow.com/search?q=%5Bjavascript%5D+how+to+validate+a+date). The short answer is no, you can only determine if a timestamps with a specific format is valid or not. That's it. Many timestamps fit multiple formats and might be valid in one but not another. – RobG Jul 12 '23 at 03:58

2 Answers2

-2

Creating a date object by using the method referenced: new Date() accepts valid date string variations by parsing the string for numeric values, so in your example is essentially using the value 1 as the date.

This will happen as long as there is at least one number. Using methods such as Date.parse() as mentioned in another comment will also give you unix value based on the parsed numeric value.

An effective method of checking for valid dates would by using a regex expression on the string.

There are various effective solutions here -> Regex to validate date formats

  • This might be a useful comment, it's not an answer. – RobG Jul 12 '23 at 06:53
  • This has been the best way I've found to do what I need. I wrote a function with all possible formats. Yes I know as some stated above that I might end up with false positives, but when you have a dynamic enterprise system deployed worldwide, this is about the best it gets – Eric Cooper Jul 12 '23 at 17:45
-2

Using Moment

visit https://momentjs.com/docs/#/parsing/ for more details

Was able to find the solution. you can use a library called moment , only providing date to moment will validate it.

var date = moment("2016-10-19");

if you want to pass a date format you can do by specifying format in 2nd argument

var date = moment("2016-10-19", "YYYY-MM-DD");

And then date.isValid() gives the desired result.

To test if a value is a date in JavaScript, you can use the Date.parse() method. The Date.parse() method attempts to parse a date string and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. If the date string is not valid, it will return NaN.

You can use the following function to check if a value is a valid date:

function isDate(value) {
  const timestamp = Date.parse(value);
  return !isNaN(timestamp);
}

Now you can use the isDate() function to check if a value is a date. It will return true if the value is a valid date, and false otherwise.

Here are some examples:

console.log(isDate("MOD-001-01"));  // false
console.log(isDate("This is not a date 1"));  // false
console.log(isDate("2023-07-11"));  // true
console.log(isDate("July 11, 2023"));  // true

By using Date.parse(), you can reliably determine if a value is a date or not.

or else

You can use a regular expression to check if a value is a date.

You can use a library like Moment.js to parse and manipulate dates. These libraries provide more robust date parsing and manipulation capabilities than the built-in Date object.

pls upvote if you find this helpful

Mr CooL
  • 1
  • 1
  • 2
  • 1
    "*To reliably test if a value is a date in JavaScript, you can use the Date.parse()*". No you can't. There are 3 formats supported by ECMA-262, anything else is implementation dependent. There are any number of strings that return different results in different implementations, even different versions of the same implementation. – RobG Jul 12 '23 at 03:59
  • @RobG I agree with your point, thanks for bringing this up, I updated my solution. – Mr CooL Jul 12 '23 at 09:31