datetime class: setting day, month...

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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
    Ante Perkovic Guest

  2. Similar Questions and Discussions

    1. problem with setting up a default value for a datetime variable
      OK, I am using MySQL Administrator 1.1.9 and MySQL 5.0.19 on Windows XP Pro. I am setting up a table which I will routinely populate by uploading...
    2. Problem setting and accessing class members
      Hello, I have the following piece of php code: <code snippet> class Section { var $id; var $name; var $color;
    3. setting variables inside a php class
      I am trying to change the value of a variable inside a class from one of its functions rather than the constructor, but it doesn't seem to work. ...
    4. to convert a month to previous month
      How to convert a month to previous month in a very easy way? For example, I have AUGUST, but I want JULY to return. Thanks. *** Sent via...
    5. Trouble setting the web.config to use my class
      Hey Everyone, I am building a store application that is on an unsecured server. We got one of those shared certificates that takes you from (ex....
  3. #2

    Default 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:toqrfvsiq84uu4v45ob7akek9hpa7p99ho@4ax.com...
    > 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 Guest

  4. #3

    Default Re: datetime class: setting day, month...

    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in message news:<e5n1TBfPDHA.2636@TK2MSFTNGP10.phx.gbl>...
    > 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 begginingOfThisMonth 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 or
    > > last day of this month or any other date.
    Ante Perkovic Guest

  5. #4

    Default Re: datetime class: setting day, month...

    "Chris Becker" <slolife@hotmail.com> wrote in message news:<#rJv2G2PDHA.2636@TK2MSFTNGP10.phx.gbl>...
    > 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 GetFirstDateOfMonth(DateTime date)
    > {
    > return (DateTime.Parse(date.Month.ToString() + "/01/" +
    > date.Year.ToString()));
    > } ...
    Thanks, Chris

    Cool :), but M$ should have done this instead of You!

    Thanks,
    Ante
    Ante Perkovic Guest

  6. #5

    Default 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 GetFirstDateOfMonth(DateTime date) {
    return (new DateTime( date.Year, date.Month, 1));
    }

    static public DateTime GetLastDateOfMonth( DateTime date) {
    return (new DateTime( date.Year, date.Month,
    DateTime.DaysInMonth( date.Year, date.Month)));
    }



    "Chris Becker" <slolife@hotmail.com> wrote in message
    news:#rJv2G2PDHA.2636@TK2MSFTNGP10.phx.gbl...
    > 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 GetFirstDateOfMonth(DateTime date)
    > {
    > return (DateTime.Parse(date.Month.ToString() + "/01/" +
    > date.Year.ToString()));
    > }
    >
    > /// <summary>
    > /// Determines the first date of the month/year specified
    > /// </summary>
    > /// <param name="Month">Number of month (1 - 12) desired</param>
    > /// <param name="Year">Year (YYYY) of month desired</param>
    > /// <returns>A DateTime value of the first date of the month
    > specified</returns>
    > static public DateTime GetFirstDateOfMonth(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 GetLastDateOfMonth(DateTime date)
    > {
    > return (DateTime.Parse(date.Month.ToString() + "/" +
    > DateTime.DaysInMonth(date.Year, date.Month).ToString() + "/" +
    > date.Year.ToString()));
    > }
    >
    > /// <summary>
    > /// Determines the last date of the month/year specified
    > /// </summary>
    > /// <param name="Month">Number of month (1 - 12) desired</param>
    > /// <param name="Year">Year (YYYY) of month desired</param>
    > /// <returns>A DateTime value of the last date of the month
    > specified</returns>
    > static public DateTime GetLastDateOfMonth(int month, int year)
    > {
    > return (DateTime.Parse(month.ToString() + "/" +
    DateTime.DaysInMonth(year,
    > month).ToString() + "/" + year.ToString()));
    > }
    >
    > "Ante Perkovic" <anteperkovic@vip.hr> wrote in message
    > news:f7ead98e.0306290342.1620f08e@posting.google.c om...
    > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in
    > message news:<e5n1TBfPDHA.2636@TK2MSFTNGP10.phx.gbl>...
    > > > 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 begginingOfThisMonth 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 or
    > > > > last day of this month or any other date.
    >
    >

    MikeB Guest

  7. #6

    Default 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 = GetFirstDateOfMonth(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:etMGDCCQDHA.3088@TK2MSFTNGP10.phx.gbl...
    > 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 GetFirstDateOfMonth(DateTime date) {
    > return (new DateTime( date.Year, date.Month, 1));
    > }
    >
    > static public DateTime GetLastDateOfMonth( DateTime date) {
    > return (new DateTime( date.Year, date.Month,
    > DateTime.DaysInMonth( date.Year, date.Month)));
    > }
    >
    >
    >
    > "Chris Becker" <slolife@hotmail.com> wrote in message
    > news:#rJv2G2PDHA.2636@TK2MSFTNGP10.phx.gbl...
    > > 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 GetFirstDateOfMonth(DateTime date)
    > > {
    > > return (DateTime.Parse(date.Month.ToString() + "/01/" +
    > > date.Year.ToString()));
    > > }
    > >
    > > /// <summary>
    > > /// Determines the first date of the month/year specified
    > > /// </summary>
    > > /// <param name="Month">Number of month (1 - 12) desired</param>
    > > /// <param name="Year">Year (YYYY) of month desired</param>
    > > /// <returns>A DateTime value of the first date of the month
    > > specified</returns>
    > > static public DateTime GetFirstDateOfMonth(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 GetLastDateOfMonth(DateTime date)
    > > {
    > > return (DateTime.Parse(date.Month.ToString() + "/" +
    > > DateTime.DaysInMonth(date.Year, date.Month).ToString() + "/" +
    > > date.Year.ToString()));
    > > }
    > >
    > > /// <summary>
    > > /// Determines the last date of the month/year specified
    > > /// </summary>
    > > /// <param name="Month">Number of month (1 - 12) desired</param>
    > > /// <param name="Year">Year (YYYY) of month desired</param>
    > > /// <returns>A DateTime value of the last date of the month
    > > specified</returns>
    > > static public DateTime GetLastDateOfMonth(int month, int year)
    > > {
    > > return (DateTime.Parse(month.ToString() + "/" +
    > DateTime.DaysInMonth(year,
    > > month).ToString() + "/" + year.ToString()));
    > > }
    > >
    > > "Ante Perkovic" <anteperkovic@vip.hr> wrote in message
    > > news:f7ead98e.0306290342.1620f08e@posting.google.c om...
    > > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in
    > > message news:<e5n1TBfPDHA.2636@TK2MSFTNGP10.phx.gbl>...
    > > > > 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 begginingOfThisMonth 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
    or
    > > > > > last day of this month or any other date.
    > >
    > >
    >
    >

    Chris Becker Guest

  8. #7

    Default Re: datetime class: setting day, month...

    "MikeB" <mailbox.google@mailnull.com> wrote in message news:<etMGDCCQDHA.3088@TK2MSFTNGP10.phx.gbl>...
    > 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 GetFirstDateOfMonth(DateTime date) {
    > return (new DateTime( date.Year, date.Month, 1));
    > }
    >
    > static public DateTime GetLastDateOfMonth( DateTime date) {
    > return (new DateTime( date.Year, date.Month,
    > DateTime.DaysInMonth( date.Year, date.Month)));
    > }
    This sounds simple enough.

    Thanks to all of you :)

    Ante
    Ante Perkovic Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139