datetime class: setting day, month...

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
 
P

Paul

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
 
A

Ante Perkovic

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?

 
C

Chris Becker

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?

 
A

Ante Perkovic

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
 
M

MikeB

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.
 
C

Chris Becker

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.
 
A

Ante Perkovic

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,124
Latest member
JuniorPell
Top