Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. How to handle dates _properly_. [modified]

How to handle dates _properly_. [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialjsonquestion
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #1

    First, you use a varchar(20) default '' to store a six char date in the form yymmdd.  Users have the luxury of an extra fourteen chars to add willy nilly romantic annotations to the date.  Savvy data entry clerks may even squeeze a well formed XHTML tag in there. Then, when the huns appear over the next control-break horizon, and you need the real DateTime you promised them, you divide and conquer.  A bunch of little two character numbers quickly evaporate into two character numbers.  Except the year pair, '09'.  In the shocking assault on decency everywhere, teh geinus grugru of date parsing expertly recognised that the '09' needed '20'.  Why waste two innocent zeros and add '2000', when you can add '20', as in string conctratingation, but hey, strings are for physicists and yoyos, so lets just add 20.  In the following horrific, true life example of unprecedented savagery, our valiant knight takes a string value for detailLastDate; of '090722' and with a wave of a hanky and quickly stuffing a rabbit into a hat for distraction, presents The Date Parsing canon (emphasis mine):

    DateTime time2 = new DateTime(int.Parse(20 + detailLastDate.Substring(0, 2)), int.Parse(detailLastDate.Substring(2, 2)), int.Parse(detailLastDate.Substring(4, 2)));

    Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009?  I am forever in awe.


    Last modified: 9hrs 10mins after originally posted --

    B K S C V 5 Replies Last reply
    0
    • B Brady Kelly

      First, you use a varchar(20) default '' to store a six char date in the form yymmdd.  Users have the luxury of an extra fourteen chars to add willy nilly romantic annotations to the date.  Savvy data entry clerks may even squeeze a well formed XHTML tag in there. Then, when the huns appear over the next control-break horizon, and you need the real DateTime you promised them, you divide and conquer.  A bunch of little two character numbers quickly evaporate into two character numbers.  Except the year pair, '09'.  In the shocking assault on decency everywhere, teh geinus grugru of date parsing expertly recognised that the '09' needed '20'.  Why waste two innocent zeros and add '2000', when you can add '20', as in string conctratingation, but hey, strings are for physicists and yoyos, so lets just add 20.  In the following horrific, true life example of unprecedented savagery, our valiant knight takes a string value for detailLastDate; of '090722' and with a wave of a hanky and quickly stuffing a rabbit into a hat for distraction, presents The Date Parsing canon (emphasis mine):

      DateTime time2 = new DateTime(int.Parse(20 + detailLastDate.Substring(0, 2)), int.Parse(detailLastDate.Substring(2, 2)), int.Parse(detailLastDate.Substring(4, 2)));

      Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009?  I am forever in awe.


      Last modified: 9hrs 10mins after originally posted --

      B Offline
      B Offline
      Bernard Laplace
      wrote on last edited by
      #2

      Not sure about that, but I can feel some kind of irony in this post... :laugh:

      1 Reply Last reply
      0
      • B Brady Kelly

        First, you use a varchar(20) default '' to store a six char date in the form yymmdd.  Users have the luxury of an extra fourteen chars to add willy nilly romantic annotations to the date.  Savvy data entry clerks may even squeeze a well formed XHTML tag in there. Then, when the huns appear over the next control-break horizon, and you need the real DateTime you promised them, you divide and conquer.  A bunch of little two character numbers quickly evaporate into two character numbers.  Except the year pair, '09'.  In the shocking assault on decency everywhere, teh geinus grugru of date parsing expertly recognised that the '09' needed '20'.  Why waste two innocent zeros and add '2000', when you can add '20', as in string conctratingation, but hey, strings are for physicists and yoyos, so lets just add 20.  In the following horrific, true life example of unprecedented savagery, our valiant knight takes a string value for detailLastDate; of '090722' and with a wave of a hanky and quickly stuffing a rabbit into a hat for distraction, presents The Date Parsing canon (emphasis mine):

        DateTime time2 = new DateTime(int.Parse(20 + detailLastDate.Substring(0, 2)), int.Parse(detailLastDate.Substring(2, 2)), int.Parse(detailLastDate.Substring(4, 2)));

        Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009?  I am forever in awe.


        Last modified: 9hrs 10mins after originally posted --

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        I suddenly feel lucky (see below)

        1 Reply Last reply
        0
        • B Brady Kelly

          First, you use a varchar(20) default '' to store a six char date in the form yymmdd.  Users have the luxury of an extra fourteen chars to add willy nilly romantic annotations to the date.  Savvy data entry clerks may even squeeze a well formed XHTML tag in there. Then, when the huns appear over the next control-break horizon, and you need the real DateTime you promised them, you divide and conquer.  A bunch of little two character numbers quickly evaporate into two character numbers.  Except the year pair, '09'.  In the shocking assault on decency everywhere, teh geinus grugru of date parsing expertly recognised that the '09' needed '20'.  Why waste two innocent zeros and add '2000', when you can add '20', as in string conctratingation, but hey, strings are for physicists and yoyos, so lets just add 20.  In the following horrific, true life example of unprecedented savagery, our valiant knight takes a string value for detailLastDate; of '090722' and with a wave of a hanky and quickly stuffing a rabbit into a hat for distraction, presents The Date Parsing canon (emphasis mine):

          DateTime time2 = new DateTime(int.Parse(20 + detailLastDate.Substring(0, 2)), int.Parse(detailLastDate.Substring(2, 2)), int.Parse(detailLastDate.Substring(4, 2)));

          Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009?  I am forever in awe.


          Last modified: 9hrs 10mins after originally posted --

          S Offline
          S Offline
          supercat9
          wrote on last edited by
          #4

          Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009? I am forever in awe. Whether or not it was reasonable for programmers in the 80's and 90's to figure their code would be obsolete before the year 2000, I see no reason to believe that any code written today will not be obsolete before the year 2100. At least the programmer deserves credit for not trying to add punctuation to convert the string to mm-dd-yy form and doing an unadorned parse on that (which would succeed or fail depending upon locale).

          D 1 Reply Last reply
          0
          • S supercat9

            Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009? I am forever in awe. Whether or not it was reasonable for programmers in the 80's and 90's to figure their code would be obsolete before the year 2000, I see no reason to believe that any code written today will not be obsolete before the year 2100. At least the programmer deserves credit for not trying to add punctuation to convert the string to mm-dd-yy form and doing an unadorned parse on that (which would succeed or fail depending upon locale).

            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #5

            supercat9 wrote:

            I see no reason to believe that any code written today will not be obsolete before the year 2100.

            I strongly disagree. IF y2k and the last decade have taught us anything it's that crufty old code doesn't die, it just gets wrapped in interface translation layers to feed newer systems.

            The European Way of War: Blow your own continent up. The American Way of War: Go over and help them.

            S 1 Reply Last reply
            0
            • B Brady Kelly

              First, you use a varchar(20) default '' to store a six char date in the form yymmdd.  Users have the luxury of an extra fourteen chars to add willy nilly romantic annotations to the date.  Savvy data entry clerks may even squeeze a well formed XHTML tag in there. Then, when the huns appear over the next control-break horizon, and you need the real DateTime you promised them, you divide and conquer.  A bunch of little two character numbers quickly evaporate into two character numbers.  Except the year pair, '09'.  In the shocking assault on decency everywhere, teh geinus grugru of date parsing expertly recognised that the '09' needed '20'.  Why waste two innocent zeros and add '2000', when you can add '20', as in string conctratingation, but hey, strings are for physicists and yoyos, so lets just add 20.  In the following horrific, true life example of unprecedented savagery, our valiant knight takes a string value for detailLastDate; of '090722' and with a wave of a hanky and quickly stuffing a rabbit into a hat for distraction, presents The Date Parsing canon (emphasis mine):

              DateTime time2 = new DateTime(int.Parse(20 + detailLastDate.Substring(0, 2)), int.Parse(detailLastDate.Substring(2, 2)), int.Parse(detailLastDate.Substring(4, 2)));

              Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009?  I am forever in awe.


              Last modified: 9hrs 10mins after originally posted --

              C Offline
              C Offline
              ClementsDan
              wrote on last edited by
              #6

              So, was this a replacement for an older program that used "19 +"?

              1 Reply Last reply
              0
              • D Dan Neely

                supercat9 wrote:

                I see no reason to believe that any code written today will not be obsolete before the year 2100.

                I strongly disagree. IF y2k and the last decade have taught us anything it's that crufty old code doesn't die, it just gets wrapped in interface translation layers to feed newer systems.

                The European Way of War: Blow your own continent up. The American Way of War: Go over and help them.

                S Offline
                S Offline
                supercat9
                wrote on last edited by
                #7

                I strongly disagree. IF y2k and the last decade have taught us anything it's that crufty old code doesn't die, it just gets wrapped in interface translation layers to feed newer systems. Code can be around for awhile, certainly, but from a user I/O perspective I don't see much point to using four-digit years for most purposes (from an internal-storage perspective, storing a year as two digits is generally silly with or without century-wrap issues). If an application is only going to deal with dates in the near future or recent past, there's no reason to require users to waste keystrokes typing a leading "20", nor is there any reason to waste space printing or displaying a "20" in front of years on paper or screen. If, seventy years from now, a printed document bears a date of 01-01-76, it will almost certainly be obvious whether the document was produced in the 1970's or 2070's. Even if there were no further improvements in printing technologies, the earliest possible ambiguities for documents printed on quality paper wouldn't occur until 2090 or so; even then, it should be obvious which documents were printed in 1990 and which ones were printed in 2090.

                1 Reply Last reply
                0
                • B Brady Kelly

                  First, you use a varchar(20) default '' to store a six char date in the form yymmdd.  Users have the luxury of an extra fourteen chars to add willy nilly romantic annotations to the date.  Savvy data entry clerks may even squeeze a well formed XHTML tag in there. Then, when the huns appear over the next control-break horizon, and you need the real DateTime you promised them, you divide and conquer.  A bunch of little two character numbers quickly evaporate into two character numbers.  Except the year pair, '09'.  In the shocking assault on decency everywhere, teh geinus grugru of date parsing expertly recognised that the '09' needed '20'.  Why waste two innocent zeros and add '2000', when you can add '20', as in string conctratingation, but hey, strings are for physicists and yoyos, so lets just add 20.  In the following horrific, true life example of unprecedented savagery, our valiant knight takes a string value for detailLastDate; of '090722' and with a wave of a hanky and quickly stuffing a rabbit into a hat for distraction, presents The Date Parsing canon (emphasis mine):

                  DateTime time2 = new DateTime(int.Parse(20 + detailLastDate.Substring(0, 2)), int.Parse(detailLastDate.Substring(2, 2)), int.Parse(detailLastDate.Substring(4, 2)));

                  Isn't it wonderful how he can just add the integer 20 to the substring '09', and get a year value of 2009?  I am forever in awe.


                  Last modified: 9hrs 10mins after originally posted --

                  V Offline
                  V Offline
                  Vozzie2
                  wrote on last edited by
                  #8

                  varchar(20), you are lucky... somebody who's name i will not mention, there it's my only "programming" colleague, is coming from VB6 and now in .Net all data types are still variant(in her head). Adding numbers to strings is standard... Type Casting? What is that? Turning on option strict results in hundreds of errors , in one project with one source file, of 3000 lines... and tables, don't ask ... today my heart stopped a moment with this... CREATE TABLE `tkolkinfo` ( `tkolkIK` int(11) NOT NULL auto_increment, `tKolkClientId` varchar(100) NOT NULL default '', `tKolkTimestampBerichtOpmaak` varchar(100) NOT NULL default '', `tKolkTimestampDateKolkDone` varchar(100) NOT NULL default '', `tKolkTimestampHourKolkDone` varchar(100) NOT NULL default '', `tKolkTimestampDateTimeKolkDone` varchar(100) NOT NULL default '', But at least in the company where i work next to .Net there is windev, helping me to forget all the IT horror and dream away on a beach with a beautifull girl: http://www.pcsoft-windev-webdev.com/WD14brochure.pdf[^] (this was sarcasm for the ones who didn't notice...)

                  It feels good to learn and achieve

                  modified on Monday, August 24, 2009 10:47 PM

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups