datetime class: setting day, month...

  • Thread starter Thread starter Ante Perkovic
  • Start date Start date
A

Ante Perkovic

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
 
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
 
Jay B. Harlow said:
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?

 
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 said:
"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
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?

 
Chris Becker said:
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
 
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 said:
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 said:
"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
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.
 
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 said:
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 said:
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 said:
"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
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 said:
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
 
Back
Top