datetime class: setting day, month...

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

    datetime class: setting day, month...

    Hi,

    How to declare datetime object and set it to my birthday, first or
    last day of this month or any other date.

    I can't find any examples in VS.NET help!

    BTW, what is the difference between date and datetime classes?

    Please, help

    Ante
  • Paul

    #2
    Re: datetime class: setting day, month...

    In VB.NET...
    Dim dtBirthday as DateTime = "6 November 1981 00:00"

    I think the difference between the 2 classes are their precision. Date
    class literally just holds the date (6 Novemebr 1981), whereas the DateTime
    class holds (6 November 1981 13:42). I imagine imagine it's to do with how
    much memory is reserved. I might be talking rubbish, it's just one of those
    things I took for granted.

    Paul
    "Ante Perkovic" <albirejo_minus _j@vip.hr> wrote in message
    news:toqrfvsiq8 4uu4v45ob7akek9 hpa7p99ho@4ax.c om...[color=blue]
    > Hi,
    >
    > How to declare datetime object and set it to my birthday, first or
    > last day of this month or any other date.
    >
    > I can't find any examples in VS.NET help!
    >
    > BTW, what is the difference between date and datetime classes?
    >
    > Please, help
    >
    > Ante[/color]


    Comment

    • Ante Perkovic

      #3
      Re: datetime class: setting day, month...

      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message news:<e5n1TBfPD HA.2636@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
      > Dim birthday As New DateTime(2003, 6, 28)[/color]
      ....[color=blue]
      > Dim birthday As DateTime = #6/28/2003#[/color]
      ....
      [color=blue]
      > Hope this helps
      > Jay[/color]

      Yes, thanks.

      But, I still don't know how to easily set date to the first day of
      this month (if I don't know which is the current month).

      I need something like this:
      Dim begginingOfThis Month As Date = DateTime.Today. setDay(1)

      Is there an easy way to do this?

      [color=blue][color=green]
      > > How to declare datetime object and set it to my birthday, first or
      > > last day of this month or any other date.[/color][/color]

      Comment

      • Chris Becker

        #4
        Re: datetime class: setting day, month...

        Here's a some functions I wrote. There may be faster ways to do this:

        /// <summary>
        /// Determines the first date of the month/year specified
        /// </summary>
        /// <param name="date">Any DateTime value that is in the month
        desired</param>
        /// <returns>A DateTime value of the first date of the month
        specified</returns>
        static public DateTime GetFirstDateOfM onth(DateTime date)
        {
        return (DateTime.Parse (date.Month.ToS tring() + "/01/" +
        date.Year.ToStr ing()));
        }

        /// <summary>
        /// Determines the first date of the month/year specified
        /// </summary>
        /// <param name="Month">Nu mber of month (1 - 12) desired</param>
        /// <param name="Year">Yea r (YYYY) of month desired</param>
        /// <returns>A DateTime value of the first date of the month
        specified</returns>
        static public DateTime GetFirstDateOfM onth(int month, int year)
        {
        return (DateTime.Parse (month.ToString () + "/01/" + year.ToString() ));
        }

        /// <summary>
        /// Determines the last date of the month/year specified
        /// </summary>
        /// <param name="date">Any DateTime value that is in the month
        desired</param>
        /// <returns>A DateTime value of the last date of the month
        specified</returns>
        static public DateTime GetLastDateOfMo nth(DateTime date)
        {
        return (DateTime.Parse (date.Month.ToS tring() + "/" +
        DateTime.DaysIn Month(date.Year , date.Month).ToS tring() + "/" +
        date.Year.ToStr ing()));
        }

        /// <summary>
        /// Determines the last date of the month/year specified
        /// </summary>
        /// <param name="Month">Nu mber of month (1 - 12) desired</param>
        /// <param name="Year">Yea r (YYYY) of month desired</param>
        /// <returns>A DateTime value of the last date of the month
        specified</returns>
        static public DateTime GetLastDateOfMo nth(int month, int year)
        {
        return (DateTime.Parse (month.ToString () + "/" + DateTime.DaysIn Month(year,
        month).ToString () + "/" + year.ToString() ));
        }

        "Ante Perkovic" <anteperkovic@v ip.hr> wrote in message
        news:f7ead98e.0 306290342.1620f 08e@posting.goo gle.com...[color=blue]
        > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
        message news:<e5n1TBfPD HA.2636@TK2MSFT NGP10.phx.gbl>. ..[color=blue][color=green]
        > > Dim birthday As New DateTime(2003, 6, 28)[/color]
        > ...[color=green]
        > > Dim birthday As DateTime = #6/28/2003#[/color]
        > ...
        >[color=green]
        > > Hope this helps
        > > Jay[/color]
        >
        > Yes, thanks.
        >
        > But, I still don't know how to easily set date to the first day of
        > this month (if I don't know which is the current month).
        >
        > I need something like this:
        > Dim begginingOfThis Month As Date = DateTime.Today. setDay(1)
        >
        > Is there an easy way to do this?
        >
        >[color=green][color=darkred]
        > > > How to declare datetime object and set it to my birthday, first or
        > > > last day of this month or any other date.[/color][/color][/color]


        Comment

        • MikeB

          #5
          Re: datetime class: setting day, month...

          You might want to use one of DateTime's constructors instead of parsing the
          date strings. This will certainly be faster, and more importantly will
          insulate your code from variations in different cultures' date formats.

          For example:

          static public DateTime GetFirstDateOfM onth(DateTime date) {
          return (new DateTime( date.Year, date.Month, 1));
          }

          static public DateTime GetLastDateOfMo nth( DateTime date) {
          return (new DateTime( date.Year, date.Month,
          DateTime.DaysIn Month( date.Year, date.Month)));
          }



          "Chris Becker" <slolife@hotmai l.com> wrote in message
          news:#rJv2G2PDH A.2636@TK2MSFTN GP10.phx.gbl...[color=blue]
          > Here's a some functions I wrote. There may be faster ways to do this:
          >
          > /// <summary>
          > /// Determines the first date of the month/year specified
          > /// </summary>
          > /// <param name="date">Any DateTime value that is in the month
          > desired</param>
          > /// <returns>A DateTime value of the first date of the month
          > specified</returns>
          > static public DateTime GetFirstDateOfM onth(DateTime date)
          > {
          > return (DateTime.Parse (date.Month.ToS tring() + "/01/" +
          > date.Year.ToStr ing()));
          > }
          >
          > /// <summary>
          > /// Determines the first date of the month/year specified
          > /// </summary>
          > /// <param name="Month">Nu mber of month (1 - 12) desired</param>
          > /// <param name="Year">Yea r (YYYY) of month desired</param>
          > /// <returns>A DateTime value of the first date of the month
          > specified</returns>
          > static public DateTime GetFirstDateOfM onth(int month, int year)
          > {
          > return (DateTime.Parse (month.ToString () + "/01/" + year.ToString() ));
          > }
          >
          > /// <summary>
          > /// Determines the last date of the month/year specified
          > /// </summary>
          > /// <param name="date">Any DateTime value that is in the month
          > desired</param>
          > /// <returns>A DateTime value of the last date of the month
          > specified</returns>
          > static public DateTime GetLastDateOfMo nth(DateTime date)
          > {
          > return (DateTime.Parse (date.Month.ToS tring() + "/" +
          > DateTime.DaysIn Month(date.Year , date.Month).ToS tring() + "/" +
          > date.Year.ToStr ing()));
          > }
          >
          > /// <summary>
          > /// Determines the last date of the month/year specified
          > /// </summary>
          > /// <param name="Month">Nu mber of month (1 - 12) desired</param>
          > /// <param name="Year">Yea r (YYYY) of month desired</param>
          > /// <returns>A DateTime value of the last date of the month
          > specified</returns>
          > static public DateTime GetLastDateOfMo nth(int month, int year)
          > {
          > return (DateTime.Parse (month.ToString () + "/" +[/color]
          DateTime.DaysIn Month(year,[color=blue]
          > month).ToString () + "/" + year.ToString() ));
          > }
          >
          > "Ante Perkovic" <anteperkovic@v ip.hr> wrote in message
          > news:f7ead98e.0 306290342.1620f 08e@posting.goo gle.com...[color=green]
          > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
          > message news:<e5n1TBfPD HA.2636@TK2MSFT NGP10.phx.gbl>. ..[color=green][color=darkred]
          > > > Dim birthday As New DateTime(2003, 6, 28)[/color]
          > > ...[color=darkred]
          > > > Dim birthday As DateTime = #6/28/2003#[/color]
          > > ...
          > >[color=darkred]
          > > > Hope this helps
          > > > Jay[/color]
          > >
          > > Yes, thanks.
          > >
          > > But, I still don't know how to easily set date to the first day of
          > > this month (if I don't know which is the current month).
          > >
          > > I need something like this:
          > > Dim begginingOfThis Month As Date = DateTime.Today. setDay(1)
          > >
          > > Is there an easy way to do this?
          > >
          > >[color=darkred]
          > > > > How to declare datetime object and set it to my birthday, first or
          > > > > last day of this month or any other date.[/color][/color]
          >
          >[/color]


          Comment

          • Ante Perkovic

            #6
            Re: datetime class: setting day, month...

            "MikeB" <mailbox.google @mailnull.com> wrote in message news:<etMGDCCQD HA.3088@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
            > You might want to use one of DateTime's constructors instead of parsing the
            > date strings. This will certainly be faster, and more importantly will
            > insulate your code from variations in different cultures' date formats.
            >
            > For example:
            >
            > static public DateTime GetFirstDateOfM onth(DateTime date) {
            > return (new DateTime( date.Year, date.Month, 1));
            > }
            >
            > static public DateTime GetLastDateOfMo nth( DateTime date) {
            > return (new DateTime( date.Year, date.Month,
            > DateTime.DaysIn Month( date.Year, date.Month)));
            > }[/color]

            This sounds simple enough.

            Thanks to all of you :)

            Ante

            Comment

            • Chris Becker

              #7
              Re: datetime class: setting day, month...

              I noticed that too when I looked at the code again to post the message. I
              have changed it to the same code you posted. Thanks MikeB

              I also thought I'd post another piece of code that might be helpful when
              working with dates:

              /// <summary>
              /// Determine which week of the month the specified date is in
              /// </summary>
              /// <param name="date">The date to determine the week of the month</param>
              /// <returns>The week of the month that the date occurs in</returns>
              static public int GetWeekOfMonth( DateTime date)
              {
              DateTime pos = GetFirstDateOfM onth(date);
              int rank = 1;
              while (pos < date)
              {
              pos = pos.AddDays(7);
              ++rank;
              }
              if (pos > date)
              --rank;
              return (rank);
              }

              "MikeB" <mailbox.google @mailnull.com> wrote in message
              news:etMGDCCQDH A.3088@TK2MSFTN GP10.phx.gbl...[color=blue]
              > You might want to use one of DateTime's constructors instead of parsing[/color]
              the[color=blue]
              > date strings. This will certainly be faster, and more importantly will
              > insulate your code from variations in different cultures' date formats.
              >
              > For example:
              >
              > static public DateTime GetFirstDateOfM onth(DateTime date) {
              > return (new DateTime( date.Year, date.Month, 1));
              > }
              >
              > static public DateTime GetLastDateOfMo nth( DateTime date) {
              > return (new DateTime( date.Year, date.Month,
              > DateTime.DaysIn Month( date.Year, date.Month)));
              > }
              >
              >
              >
              > "Chris Becker" <slolife@hotmai l.com> wrote in message
              > news:#rJv2G2PDH A.2636@TK2MSFTN GP10.phx.gbl...[color=green]
              > > Here's a some functions I wrote. There may be faster ways to do this:
              > >
              > > /// <summary>
              > > /// Determines the first date of the month/year specified
              > > /// </summary>
              > > /// <param name="date">Any DateTime value that is in the month
              > > desired</param>
              > > /// <returns>A DateTime value of the first date of the month
              > > specified</returns>
              > > static public DateTime GetFirstDateOfM onth(DateTime date)
              > > {
              > > return (DateTime.Parse (date.Month.ToS tring() + "/01/" +
              > > date.Year.ToStr ing()));
              > > }
              > >
              > > /// <summary>
              > > /// Determines the first date of the month/year specified
              > > /// </summary>
              > > /// <param name="Month">Nu mber of month (1 - 12) desired</param>
              > > /// <param name="Year">Yea r (YYYY) of month desired</param>
              > > /// <returns>A DateTime value of the first date of the month
              > > specified</returns>
              > > static public DateTime GetFirstDateOfM onth(int month, int year)
              > > {
              > > return (DateTime.Parse (month.ToString () + "/01/" + year.ToString() ));
              > > }
              > >
              > > /// <summary>
              > > /// Determines the last date of the month/year specified
              > > /// </summary>
              > > /// <param name="date">Any DateTime value that is in the month
              > > desired</param>
              > > /// <returns>A DateTime value of the last date of the month
              > > specified</returns>
              > > static public DateTime GetLastDateOfMo nth(DateTime date)
              > > {
              > > return (DateTime.Parse (date.Month.ToS tring() + "/" +
              > > DateTime.DaysIn Month(date.Year , date.Month).ToS tring() + "/" +
              > > date.Year.ToStr ing()));
              > > }
              > >
              > > /// <summary>
              > > /// Determines the last date of the month/year specified
              > > /// </summary>
              > > /// <param name="Month">Nu mber of month (1 - 12) desired</param>
              > > /// <param name="Year">Yea r (YYYY) of month desired</param>
              > > /// <returns>A DateTime value of the last date of the month
              > > specified</returns>
              > > static public DateTime GetLastDateOfMo nth(int month, int year)
              > > {
              > > return (DateTime.Parse (month.ToString () + "/" +[/color]
              > DateTime.DaysIn Month(year,[color=green]
              > > month).ToString () + "/" + year.ToString() ));
              > > }
              > >
              > > "Ante Perkovic" <anteperkovic@v ip.hr> wrote in message
              > > news:f7ead98e.0 306290342.1620f 08e@posting.goo gle.com...[color=darkred]
              > > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
              > > message news:<e5n1TBfPD HA.2636@TK2MSFT NGP10.phx.gbl>. ..[color=darkred]
              > > > > Dim birthday As New DateTime(2003, 6, 28)
              > > > ...
              > > > > Dim birthday As DateTime = #6/28/2003#
              > > > ...
              > > >
              > > > > Hope this helps
              > > > > Jay
              > > >
              > > > Yes, thanks.
              > > >
              > > > But, I still don't know how to easily set date to the first day of
              > > > this month (if I don't know which is the current month).
              > > >
              > > > I need something like this:
              > > > Dim begginingOfThis Month As Date = DateTime.Today. setDay(1)
              > > >
              > > > Is there an easy way to do this?
              > > >
              > > >
              > > > > > How to declare datetime object and set it to my birthday, first[/color][/color][/color]
              or[color=blue][color=green][color=darkred]
              > > > > > last day of this month or any other date.[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...