By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

datetime class: setting day, month...

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <al**************@vip.hr> wrote in message
news:to********************************@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

Nov 17 '05 #2

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:<e5**************@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.

Nov 17 '05 #3

P: n/a
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" <an**********@vip.hr> wrote in message
news:f7**************************@posting.google.c om...
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message news:<e5**************@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.

Nov 17 '05 #4

P: n/a
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" <sl*****@hotmail.com> wrote in message
news:#r**************@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" <an**********@vip.hr> wrote in message
news:f7**************************@posting.google.c om...
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message news:<e5**************@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.


Nov 17 '05 #5

P: n/a
"MikeB" <ma************@mailnull.com> wrote in message news:<et**************@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
Nov 17 '05 #6

P: n/a
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" <ma************@mailnull.com> wrote in message
news:et**************@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" <sl*****@hotmail.com> wrote in message
news:#r**************@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" <an**********@vip.hr> wrote in message
news:f7**************************@posting.google.c om...
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message news:<e5**************@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.



Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.