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. A new way to create a DateTime from the year!

A new way to create a DateTime from the year!

Scheduled Pinned Locked Moved The Weird and The Wonderful
help
8 Posts 5 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.
  • S Offline
    S Offline
    sergiogarcianinja
    wrote on last edited by
    #1

    Looking for a error in a project, I saw this code.

    IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
    string year = itemDT["YEARBARRIER"].ToString();

    string dateYear = "01" + "/" + "01" + "/" + year;

    DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);

    objBarrierFacade.YearBarrier = convertedDate;

    That can be writed with only one line.

    objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);

    But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.

    J C D 3 Replies Last reply
    0
    • S sergiogarcianinja

      Looking for a error in a project, I saw this code.

      IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
      string year = itemDT["YEARBARRIER"].ToString();

      string dateYear = "01" + "/" + "01" + "/" + year;

      DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);

      objBarrierFacade.YearBarrier = convertedDate;

      That can be writed with only one line.

      objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);

      But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.

      J Offline
      J Offline
      Jaime Olivares
      wrote on last edited by
      #2

      my rule of thumb is: when some problem needs a lot of coding, there should be a shorter way to do it. Look for it! Software library functionalities like .net's are based on the survey of the necessities of millions of developers. This rule, as every one, has some exceptions.

      Best regards, Jaime.

      S 1 Reply Last reply
      0
      • S sergiogarcianinja

        Looking for a error in a project, I saw this code.

        IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
        string year = itemDT["YEARBARRIER"].ToString();

        string dateYear = "01" + "/" + "01" + "/" + year;

        DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);

        objBarrierFacade.YearBarrier = convertedDate;

        That can be writed with only one line.

        objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);

        But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.

        C Offline
        C Offline
        Chris Maunder
        wrote on last edited by
        #3

        Yes, kind of, give or take some input data checks.

        cheers, Chris Maunder The Code Project Co-founder Microsoft C++ MVP

        D 1 Reply Last reply
        0
        • S sergiogarcianinja

          Looking for a error in a project, I saw this code.

          IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
          string year = itemDT["YEARBARRIER"].ToString();

          string dateYear = "01" + "/" + "01" + "/" + year;

          DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);

          objBarrierFacade.YearBarrier = convertedDate;

          That can be writed with only one line.

          objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);

          But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          The only comment I would make is that you have dropped the CultureInfo from your solution. Not sure how important that is for your project, but it could be significant. For example, under the Islamic calendar the current year is 1430. That would give a very different DateTime value than the Gregorian calendar used in the US.

          S 1 Reply Last reply
          0
          • D David Skelly

            The only comment I would make is that you have dropped the CultureInfo from your solution. Not sure how important that is for your project, but it could be significant. For example, under the Islamic calendar the current year is 1430. That would give a very different DateTime value than the Gregorian calendar used in the US.

            S Offline
            S Offline
            sergiogarcianinja
            wrote on last edited by
            #5

            I now that. But as you can see, the developer forced a CultureInfo to ensure a date format valid to parse his string. In the case explaned this is not necessary because the developer only need to create a datetime object instace from a int value representing a year.

            D 1 Reply Last reply
            0
            • J Jaime Olivares

              my rule of thumb is: when some problem needs a lot of coding, there should be a shorter way to do it. Look for it! Software library functionalities like .net's are based on the survey of the necessities of millions of developers. This rule, as every one, has some exceptions.

              Best regards, Jaime.

              S Offline
              S Offline
              sergiogarcianinja
              wrote on last edited by
              #6

              Yeah, it's the objective of this post. Some programmer use a lot of code because they don't know a easier way to do that.

              1 Reply Last reply
              0
              • S sergiogarcianinja

                I now that. But as you can see, the developer forced a CultureInfo to ensure a date format valid to parse his string. In the case explaned this is not necessary because the developer only need to create a datetime object instace from a int value representing a year.

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                ...unless you run the code with a different default Culture, in which case you may get a different DateTime value for the same year input depending on the default calendar in use. The original code will always produce the same result since it specifies which CultureInfo to use. I'm not saying the original code is right, I am merely pointing out that strictly speaking your replacement code is not functionally equivalent to the original. Probably not an issue if your code will never run outside the US but, hey, I'm a geek and pedantry is part of the lifestyle.

                1 Reply Last reply
                0
                • C Chris Maunder

                  Yes, kind of, give or take some input data checks.

                  cheers, Chris Maunder The Code Project Co-founder Microsoft C++ MVP

                  D Offline
                  D Offline
                  dojohansen
                  wrote on last edited by
                  #8

                  Huh? How do you know there is input data involved here or that it's not known to be in a valid state?

                  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