Date datatype

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cristian Martinello

    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
  • Lasse Reichstein Nielsen

    #2
    Re: Date datatype

    "Cristian Martinello" <cattivofics@ti scali.it> writes:
    [color=blue]
    > There's a way to check if a string is a valid date ?[/color]

    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 - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Cristian Martinello

      #3
      Re: Date datatype

      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:y8vmsrxk.f sf@hotpop.com
      [color=blue]
      > "Cristian Martinello" <cattivofics@ti scali.it> writes:
      >[color=green]
      > > There's a way to check if a string is a valid date ?[/color]
      >
      > That depends on what you consider a valid date.
      >
      > I would consider 03/03/2003 to be invalid, because it is ambiguous.
      >[/color]

      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

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Date datatype

        Cristian Martinello wrote:[color=blue]
        > There's a way to check if a string is a valid date ?[/color]

        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;time.js@Poi ntedEars.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

        Comment

        • Laurent Bugnion, GalaSoft

          #5
          Re: Date datatype

          Hi,

          Cristian Martinello wrote:
          [color=blue]
          > "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          > news:y8vmsrxk.f sf@hotpop.com
          >
          >[color=green]
          >>"Cristian Martinello" <cattivofics@ti scali.it> writes:
          >>
          >>[color=darkred]
          >>>There's a way to check if a string is a valid date ?[/color]
          >>
          >>That depends on what you consider a valid date.
          >>
          >>I would consider 03/03/2003 to be invalid, because it is ambiguous.
          >>[/color]
          >
          >
          > 03/03/2003 it's a valid date, also 03/03/03, it's 3rd march 2003.[/color]

          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

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Date datatype

            "Cristian Martinello" <cattivofics@ti scali.it> writes:
            [color=blue]
            > 03/03/2003 it's a valid date, also 03/03/03, it's 3rd march 2003.[/color]

            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 - lrn@hotpop.com
            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Date datatype

              Cristian Martinello wrote:
              [color=blue]
              > There's a way to check if a string is a valid date ?[/color]

              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;time.js@Poi ntedEars.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

              Comment

              • Dr John Stockton

                #8
                Re: Date datatype

                JRS: In article <y8vmsrxk.fsf@h otpop.com>, seen in
                news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                posted at Wed, 15 Oct 2003 12:42:31 :-
                [color=blue]
                >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[/color]

                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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                Comment

                • Dr John Stockton

                  #9
                  Re: Date datatype

                  JRS: In article <3F8D4057.40209 04@PointedEars. de>, seen in
                  news:comp.lang. javascript, Thomas 'PointedEars' Lahn
                  <PointedEars@we b.de> posted at Wed, 15 Oct 2003 14:40:55 :-[color=blue]
                  >Cristian Martinello wrote:
                  >[color=green]
                  >> There's a way to check if a string is a valid date ?[/color][/color]
                  [color=blue]
                  >function isValidDate([/color]
                  [color=blue]
                  > var
                  > d = new Date(iYear, iMonth, iDate), // create new Date object
                  > y = 0;
                  >
                  > if (d)[/color]

                  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).
                  [color=blue]
                  > {
                  > y =
                  > (d.getFullYear // prefer 4-digit year
                  > ? d.getFullYear()
                  > : d.getYear());
                  > if (!d.getFullYear && y < 1900) // Y2K workaround[/color]

                  Better to use y < 1000, which should give a range of 1000-2899 ??
                  Untested.
                  [color=blue]
                  > y += 1900;
                  > }[/color]
                  [color=blue]
                  > return (
                  > d && y == iYear && d.getMonth() == iMonth && d.getDate() == iDate);
                  >}[/color]

                  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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                  Comment

                  Working...