473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Date datatype

There's a way to check if a string is a valid date ?
Thanks helpers.
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 20 '05 #1
8 5171
"Cristian Martinello" <ca*********@tiscali.it> writes:
There's a way to check if a string is a valid date ?


That depends on what you consider a valid date.

I would consider 03/03/2003 to be invalid, because it is ambiguous.

Is "Mit 15. Okt 2003" valid? It is in Germany.

Obviously, you can throw it at the Date constructor and see what
happens. That is not very conclusive. E.g.,
new Date("argle bargle 24 zippo 11 wizzo 15")
gives a date that reports itself as
Fri, 24 Oct 2003 12:45:00 GMT+0200
(It seems to subtract the 15. Change it to 21 and the result is
Fri, 24 Oct 2003 12:39:00 GMT+0200
Highly curious.)
In one sense it is "valid". In another, it is garbage. I would
go for the latter. :)

*You* need to decide what an *acceptable* date is. Then I am sure we
can find a way to validate it.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:y8**********@hotpop.com
"Cristian Martinello" <ca*********@tiscali.it> writes:
There's a way to check if a string is a valid date ?


That depends on what you consider a valid date.

I would consider 03/03/2003 to be invalid, because it is ambiguous.


03/03/2003 it's a valid date, also 03/03/03, it's 3rd march 2003.
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 20 '05 #3
Cristian Martinello wrote:
There's a way to check if a string is a valid date ?


It is possible to check if a date is valid:

function isValidDate(
/** @argument number */ iYear,
/** @argument number */ iMonth,
/** @argument number */ iDate)
/**
* @author Copyright (c) 2003 Thomas Lahn &lt;ti*****@PointedEars.de&gt;
* @partof http://pointedears.de.vu/scripts/time.js
* @argdescr iYear Years since 1900. With only JavaScript 1.2
* supported, years before 1970 are not allowed.
* @argdescr iMonth Month: 0 (January) to 11 (December)
* @argdescr iDate Day of month: 1 to 31
* @returns <code>true</code> if the date is valid,
* <code>false</code> otherwise.
* @see Date()
*/
{
var
d = new Date(iYear, iMonth, iDate), // create new Date object
y = 0;

if (d)
{
y =
(d.getFullYear // prefer 4-digit year
? d.getFullYear()
: d.getYear());
if (!d.getFullYear && y < 1900) // Y2K workaround
y += 1900;
}

/*
* If no Date object exists or if the properties of the object differ
* from the passed arguments, the date was not valid (out of range,
* e.g. not paying attention to leap years.)
*/
return
(d && y == iYear && d.getMonth() == iMonth && d.getDate() == iDate);
}

Now you must specify what you consider a valid date string (i.e. how it
should be formatted), extract the components (RegExp will be useful),
pass them to the function and evaluate its result.
HTH

PointedEars

Jul 20 '05 #4
Hi,

Cristian Martinello wrote:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:y8**********@hotpop.com

"Cristian Martinello" <ca*********@tiscali.it> writes:

There's a way to check if a string is a valid date ?


That depends on what you consider a valid date.

I would consider 03/03/2003 to be invalid, because it is ambiguous.

03/03/2003 it's a valid date, also 03/03/03, it's 3rd march 2003.


And it's not even ambiguous.

03/04/2003 is ambiguous. Here, it would be 3rd of April. In the US, it
would be 4th of march.

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #5
"Cristian Martinello" <ca*********@tiscali.it> writes:
03/03/2003 it's a valid date, also 03/03/03, it's 3rd march 2003.


Doh. And I just had to pick a date that wasn't ambiguous. Yey me!
Ok. Try 04/03/2003 instead.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
Cristian Martinello wrote:
There's a way to check if a string is a valid date ?


It is possible to check if a date is valid:

function isValidDate(
/** @argument number */ iYear,
/** @argument number */ iMonth,
/** @argument number */ iDate)
/**
* @author Copyright (c) 2003 Thomas Lahn &lt;ti*****@PointedEars.de&gt;
* @partof http://pointedears.de.vu/scripts/time.js
* @argdescr iYear Years since 1900. With only JavaScript 1.2
* supported, years before 1970 are not allowed.
* @argdescr iMonth Month: 0 (January) to 11 (December)
* @argdescr iDate Day of month: 1 to 31
* @returns <code>true</code> if the date is valid,
* <code>false</code> otherwise.
* @see Date()
*/
{
var
d = new Date(iYear, iMonth, iDate), // create new Date object
y = 0;

if (d)
{
y =
(d.getFullYear // prefer 4-digit year
? d.getFullYear()
: d.getYear());
if (!d.getFullYear && y < 1900) // Y2K workaround
y += 1900;
}

/*
* If no Date object exists or if the properties of the object differ
* from the passed arguments, the date was not valid (out of range,
* e.g. not paying attention to leap years.)
*/
return (
d && y == iYear && d.getMonth() == iMonth && d.getDate() == iDate);
}

For date strings can be ambiguous, you now must specify what you consider a
valid date string (i.e. how it should be formatted), extract the components
(RegExp will be useful), check if they match your needs (maybe you're
accepting nothing before today), and if they match, pass them to the
function and evaluate its result.
HTH

PointedEars

Jul 20 '05 #7
JRS: In article <y8**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Wed, 15 Oct 2003 12:42:31 :-
Obviously, you can throw it at the Date constructor and see what
happens. That is not very conclusive. E.g.,
new Date("argle bargle 24 zippo 11 wizzo 15")
gives a date that reports itself as
Fri, 24 Oct 2003 12:45:00 GMT+0200


For you but not for me. In MSIE4, I get NaN.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8
JRS: In article <3F**************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Wed, 15 Oct 2003 14:40:55 :-
Cristian Martinello wrote:
There's a way to check if a string is a valid date ?
function isValidDate( var
d = new Date(iYear, iMonth, iDate), // create new Date object
y = 0;

if (d)
Under what circumstances does one get d undefined? So far, the worst I
get is NaN - I do not get Inf for high years (Date Object should reach
to the end of AD 275760-09-13).
{
y =
(d.getFullYear // prefer 4-digit year
? d.getFullYear()
: d.getYear());
if (!d.getFullYear && y < 1900) // Y2K workaround
Better to use y < 1000, which should give a range of 1000-2899 ??
Untested.
y += 1900;
} return (
d && y == iYear && d.getMonth() == iMonth && d.getDate() == iDate);
}


AFAICS, there is no need to test all three of Y M D here.
function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d))
return ((getMonth()==m) && (getDate()==d)) /* was y, m */ }

function ReadISO8601date(Q) { var T // adaptable for other layouts
if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)$/.exec(Q)) == null)
{ return -2 } // bad format
for (var j=1; j<=5; j+=2) T[j] = +T[j] // some use needs numbers
if (!ValidDate(T[1], T[3]-1, T[5])) { return -1 } // bad value
return [ T[1], T[3], T[5] ] }

Those could no doubt be combined to return either undefined or the Date
Object. This seems to do it, in <URL:http://www.merlyn.demon.co.uk/js-
date4.htm> :

function GetISO8601date(Q) { var d, m, D, T, U // y!=0
if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)$/.exec(Q)) != null)
with (D = new Date(T[1], m=T[3]-1, d=+T[5]))
if (getMonth()==m && getDate()==d) return D
return U }
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Scarborough | last post by:
I have a Table in a Typed Dataset which contains a Date field called EventDate. I've ensured that the field is defined as Date as opposed to DateTime in the Typed Dataset. When I generate an...
7
by: Marc Pelletier | last post by:
Hello, I have a table with a Day field, defined as smalldatetime. I am filling it from a CSharp application with the following code: DataRow r = dtStaDays.NewRow(); r= station_ID; r =...
2
by: David Slinn | last post by:
I have created a simple XML document, generated a schema from it (which Visual Studio makes everything a string element). I then went through and changed all fields that are dates to the Date data...
3
by: Larry Bertolini | last post by:
For some reason, I recall having read that SQL Server 2005 would support a datatype that represented date, but not time. (This would be useful for storing things like birthday, where you usually...
3
by: Jay | last post by:
Hi , I am trying to assign a NULL value to a date (datatype). I am using VB NET 2003. I know that date cannot be assigned a null value in 2003. I cannot change the datatype as I am submitting the...
4
by: Orchid | last post by:
How can I change a Date datatype to a Number datatype? For example, I want a date 10/31/2006 to show 1031 as Number datatype. But I don't want it becomes 39021. What formula should I use? ...
10
by: meltedown | last post by:
If I have an sql date, such as "2006-11-19" is there a way using either sql or php date and time functions to get the date of the the first day of the month ?
0
by: MaartenVR | last post by:
sub: MS SQL server and (missing) ANSI DATE-datatype I’m working at a company who has developed a large client/server application in Delphi 6, with Interbase as the DB-server (both Borland...
10
by: DontellTrevell via AccessMonster.com | last post by:
HELP!!....I need to calculate the numer of days elapsed between two field. But, the date format is YYYYMMDD. How can i accomplsh this? -- Dontell Trevell Message posted via AccessMonster.com...
4
by: loisk | last post by:
Hi, I am facing some problems using date expression as string datatype. Here's clip of my code. The (Intvdate) is string datatype in the MySQL linked table. Private Sub...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.