a question regarding DateTime

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

    #1

    a question regarding DateTime

    Hey

    ..NET 2.0

    I'm about to create 2 input parameters for a method. These are 2 DateTime
    parameters - one named "from" and another named "to"... - from and to
    DateTime values...

    Okay the trick is that the "from" DateTime should be like this
    01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
    "from" date should be 01.12.2007 00:00:00. What is the best way of
    calculating this?

    any suggestions?


  • Jon Skeet [C# MVP]

    #2
    Re: a question regarding DateTime

    Jeff <donot@spam.mew rote:
    .NET 2.0
    >
    I'm about to create 2 input parameters for a method. These are 2 DateTime
    parameters - one named "from" and another named "to"... - from and to
    DateTime values...
    >
    Okay the trick is that the "from" DateTime should be like this
    01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
    "from" date should be 01.12.2007 00:00:00. What is the best way of
    calculating this?
    DateTime values don't have any inherent formatting - it's only when you
    convert them to and from strings that they're formatted.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    World class .NET training in the UK: http://iterativetraining.co.uk

    Comment

    • Jeff

      #3
      Re: a question regarding DateTime

      Okay, I've testing this solution now:

      DateTime from
      from = new DateTime(DateTi me.Now.Year, DateTime.Now.Mo nth, 1, 0, 0, 0)

      I'm debugging the code and it seems like it is working. Do you see any
      problems using this code?.


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: a question regarding DateTime

        Jeff <donot@spam.mew rote:
        Okay, I've testing this solution now:
        >
        DateTime from
        from = new DateTime(DateTi me.Now.Year, DateTime.Now.Mo nth, 1, 0, 0, 0)
        >
        I'm debugging the code and it seems like it is working. Do you see any
        problems using this code?.
        Yes - you're calling DateTime.Now twice. I know it's unlikely, but
        consider the case where it's December 31st, 23:59:59.999 - you call
        DateTime.Now and the year is 2007. Then the clock times, and then you
        call DateTime.Now and the month is January. Now you neither have Dec
        2007 nor Jan 2008 - you have Jan 2007.

        Use

        DateTime now = DateTime.Now;
        from = new DateTime(now.Ye ar, now.Month, 1, 0, 0, 0)

        (Are you sure you don't want to be using UTC, by the way? I personally
        try to keep to UTC whenever I can until the presentation layer, but I
        don't know enough about what you're doing to say for sure. Just
        something to think about.)

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        World class .NET training in the UK: http://iterativetraining.co.uk

        Comment

        • Jeff

          #5
          Re: a question regarding DateTime

          What about this approach:

          DateTime date = DateTime.Now;
          DateTime from
          from = new DateTime(date.Y ear, date.Month, 1, 0, 0, 0)


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: a question regarding DateTime

            Jeff <donot@spam.mew rote:
            What about this approach:
            >
            DateTime date = DateTime.Now;
            DateTime from
            from = new DateTime(date.Y ear, date.Month, 1, 0, 0, 0)
            Isn't that exactly what I put, just having changed the name of the
            variable from "now" to "date"?

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            World class .NET training in the UK: http://iterativetraining.co.uk

            Comment

            • Peter Duniho

              #7
              Re: a question regarding DateTime

              On Mon, 03 Dec 2007 05:15:24 -0800, Jon Skeet [C# MVP] <skeet@pobox.co m>
              wrote:
              Jeff <donot@spam.mew rote:
              >What about this approach:
              >>
              >DateTime date = DateTime.Now;
              >DateTime from
              >from = new DateTime(date.Y ear, date.Month, 1, 0, 0, 0)
              >
              Isn't that exactly what I put, just having changed the name of the
              variable from "now" to "date"?
              Logically, sure. But he fixed one of your errors (missing variable
              declaration) and introduced a new instance of the other one (missing
              semi-colon). Surely that counts for something. :) :)

              Comment

              • Liz

                #8
                Re: a question regarding DateTime


                "Jeff" <donot@spam.mew rote in message
                news:uBjMsMaNIH A.5140@TK2MSFTN GP05.phx.gbl...
                Hey
                >
                .NET 2.0
                >
                I'm about to create 2 input parameters for a method. These are 2 DateTime
                parameters - one named "from" and another named "to"... - from and to
                DateTime values...
                >
                Okay the trick is that the "from" DateTime should be like this
                01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
                "from" date should be 01.12.2007 00:00:00. What is the best way of
                calculating this?
                >
                any suggestions?
                so you want the current month.year, always prefixed with "01" and postfixed
                with "00:00:00" .. ? this should work:

                DateTime dt = DateTime.Now;
                string t = "01." + dt.Month.ToStri ng().PadLeft(2, '0') + "." +
                dt.Year.ToStrin g() + " 00:00:00";
                DateTime dt2 = Convert.ToDateT ime(t);
                don't know if it's the "best" way ... it's a way ...




                Comment

                • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

                  #9
                  Re: a question regarding DateTime

                  Liz wrote:
                  "Jeff" <donot@spam.mew rote in message
                  news:uBjMsMaNIH A.5140@TK2MSFTN GP05.phx.gbl...
                  >Hey
                  >>
                  >.NET 2.0
                  >>
                  >I'm about to create 2 input parameters for a method. These are 2 DateTime
                  >parameters - one named "from" and another named "to"... - from and to
                  >DateTime values...
                  >>
                  >Okay the trick is that the "from" DateTime should be like this
                  >01.<MM>.<YYY Y ss:mm.hh which means that today is the 03.12.2007 then the
                  >"from" date should be 01.12.2007 00:00:00. What is the best way of
                  >calculating this?
                  >>
                  >any suggestions?
                  >
                  so you want the current month.year, always prefixed with "01" and postfixed
                  with "00:00:00" .. ? this should work:
                  >
                  DateTime dt = DateTime.Now;
                  string t = "01." + dt.Month.ToStri ng().PadLeft(2, '0') + "." +
                  dt.Year.ToStrin g() + " 00:00:00";
                  DateTime dt2 = Convert.ToDateT ime(t);
                  don't know if it's the "best" way ... it's a way ...
                  >
                  >
                  >
                  >
                  That's what I call a solutionproblem , a solution with more problems than
                  it solves.

                  Go with the way that Jon posted.

                  --
                  Lasse Vågsæther Karlsen
                  mailto:lasse@vk arlsen.no
                  Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

                  Comment

                  • Liz

                    #10
                    Re: a question regarding DateTime


                    "Lasse Vågsæther Karlsen" <lasse@vkarlsen .nowrote in message
                    news:ukCn2O$NIH A.1208@TK2MSFTN GP03.phx.gbl...
                    Liz wrote:
                    >so you want the current month.year, always prefixed with "01" and
                    >postfixed with "00:00:00" .. ? this should work:
                    >>
                    >DateTime dt = DateTime.Now;
                    >string t = "01." + dt.Month.ToStri ng().PadLeft(2, '0') + "." +
                    > dt.Year.ToStrin g() + " 00:00:00";
                    >DateTime dt2 = Convert.ToDateT ime(t);
                    >don't know if it's the "best" way ... it's a way ...
                    That's what I call a solutionproblem , a solution with more problems than
                    it solves.
                    >
                    Go with the way that Jon posted.
                    I had not seen Jon's solution; now that I have, no doubt it's cleaner and I
                    would use it; compared to his, I see awkward, not problematic .. it does
                    work ... what "problems" do you see with this one other than it's slower and
                    more verbose?



                    Comment

                    • J.B. Moreno

                      #11
                      Re: a question regarding DateTime

                      In article <OvbZWHIOIHA.47 40@TK2MSFTNGP02 .phx.gbl>,
                      Liz <liz@tiredofspa m.comwrote:
                      "Lasse Vågsæther Karlsen" <lasse@vkarlsen .nowrote in message
                      Liz wrote:
                      >
                      so you want the current month.year, always prefixed with "01" and
                      postfixed with "00:00:00" .. ? this should work:
                      >
                      DateTime dt = DateTime.Now;
                      string t = "01." + dt.Month.ToStri ng().PadLeft(2, '0') + "." +
                      dt.Year.ToStrin g() + " 00:00:00";
                      DateTime dt2 = Convert.ToDateT ime(t);
                      don't know if it's the "best" way ... it's a way ...
                      >
                      >
                      That's what I call a solutionproblem , a solution with more problems than
                      it solves.

                      Go with the way that Jon posted.
                      >
                      I had not seen Jon's solution; now that I have, no doubt it's cleaner and I
                      would use it; compared to his, I see awkward, not problematic .. it does
                      work ... what "problems" do you see with this one other than it's slower and
                      more verbose?
                      Cultural info would be my guess...plus unnecessary code (there's no
                      reason to do the padding or add the time part).


                      If you're going to do a string conversion, then...

                      DateTime dt2 = DateTime.Parse( DateTime.Now.To String("yyyy/MM/01"));

                      But Jon's method (using new DateTime) is better as it avoids string
                      parsing entirely, and is thus more readable and probably faster.

                      --
                      J.B. Moreno

                      Comment

                      Working...