DayOfWeek

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

dateTime sellDate;
int16 dayOfWeek;

dayOfWeek=sellDate.DayOfWeek

I would like to set that monday is the first day of a week and not sunday.
Any idea?

In VB there is a function, where you declare which day should be the first:

DatePart(DateInterval.Weekday, sellDate, vbMonday)

Regards,
L
 
L,

You can still call this. Set a reference to Microsoft.VisualBasic.dll,
and then call the static DatePart method on the DateAndTime class in the
Microsoft.VisualBasic namespace.

Hope this helps.
 
Thank you Nicholas.

Is there any way to do that in C#?

Regards,L

Nicholas Paldino said:
L,

You can still call this. Set a reference to Microsoft.VisualBasic.dll,
and then call the static DatePart method on the DateAndTime class in the
Microsoft.VisualBasic namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

simon said:
dateTime sellDate;
int16 dayOfWeek;

dayOfWeek=sellDate.DayOfWeek

I would like to set that monday is the first day of a week and not
sunday.
Any idea?

In VB there is a function, where you declare which day should be the
first:

DatePart(DateInterval.Weekday, sellDate, vbMonday)

Regards,
L
 
simon,

Yes, exactly what I told you. It's a .NET assembly, it just happens to
be there for VB. It's distributed as part of the .NET framework, so you can
guarantee that it's going to be there on any .NET install.

Since it is a .NET assembly, you can use it in any .NET language.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

simon said:
Thank you Nicholas.

Is there any way to do that in C#?

Regards,L

Nicholas Paldino said:
L,

You can still call this. Set a reference to
Microsoft.VisualBasic.dll, and then call the static DatePart method on
the DateAndTime class in the Microsoft.VisualBasic namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

simon said:
dateTime sellDate;
int16 dayOfWeek;

dayOfWeek=sellDate.DayOfWeek

I would like to set that monday is the first day of a week and not
sunday.
Any idea?

In VB there is a function, where you declare which day should be the
first:

DatePart(DateInterval.Weekday, sellDate, vbMonday)

Regards,
L
 
Simon,
| Is there any way to do that in C#?
Yes, As Nicholas stated: add a reference to the Microsoft.VisualBasic.dll
and call Microsoft.VisualBasic.DateAndTime.DatePart!


As to your original question DateTime.DayOfWeek returns the DayOfWeek Enum
with values such as Monday, Tuesday, Wednesday, making DateTime.DayOfWeek
literal which day of the week it is, and not some offset from some arbitrary
"FirstDayOfTheWeek". So DateTime.DayOfWeek will return DayOfWeek.Monday when
its Monday!

Fortunately the DayOfWeek enum values are consecutive, so if you want to
convert the DayOfWeek enum to & from an integer value where 0 means Monday,
you can use simply comparisons & arithmetic to reimplement the aspect of
Microsoft.VisualBasic.DateAndTime.DatePart you are looking for...

Something like:

static Int16 OffsetFromFirstDayOfWeek(DateTime theDate, DayOfWeek
firstDayOfWeek)
{
Int16 dow = (Int16)((Int16)theDate.DayOfWeek -
(Int16)firstDayOfWeek);
if (dow < 0)
dow += 7;
return dow;
}


DateTime sellDate = DateTime.Now;
Int16 dayOfWeek;

dayOfWeek = OffsetFromFirstDayOfWeek(sellDate, DayOfWeek.Monday);

Alternatively you could use a switch statement if the first day of week is
fixed...

Hope this helps
Jay


| Thank you Nicholas.
|
| Is there any way to do that in C#?
|
| Regards,L
|
in
| message | > L,
| >
| > You can still call this. Set a reference to
Microsoft.VisualBasic.dll,
| > and then call the static DatePart method on the DateAndTime class in the
| > Microsoft.VisualBasic namespace.
| >
| > Hope this helps.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - (e-mail address removed)
| >
| > | >> dateTime sellDate;
| >> int16 dayOfWeek;
| >>
| >> dayOfWeek=sellDate.DayOfWeek
| >>
| >> I would like to set that monday is the first day of a week and not
| >> sunday.
| >> Any idea?
| >>
| >> In VB there is a function, where you declare which day should be the
| >> first:
| >>
| >> DatePart(DateInterval.Weekday, sellDate, vbMonday)
| >>
| >> Regards,
| >> L
| >>
| >>
| >
| >
|
|
 
Jay,

I use more-less the same technique you describe, only I use a modulus op:

dow = dow % 7;

instead of:

if (dow < 0) dow += 7;

I guess your version performs better...

Regards - Octavio
 
Octavio,
You know, in the back of my mind I was thinking modulus, but it just didn't
click when I was typing...

Thanks for the additional.

Jay

| Jay,
|
| I use more-less the same technique you describe, only I use a modulus op:
|
| dow = dow % 7;
|
| instead of:
|
| if (dow < 0) dow += 7;
|
| I guess your version performs better...
|
| Regards - Octavio
|
| "Jay B. Harlow [MVP - Outlook]" <[email protected]> escribió en el
| mensaje | > Simon,
| > | Is there any way to do that in C#?
| > Yes, As Nicholas stated: add a reference to the
Microsoft.VisualBasic.dll
| > and call Microsoft.VisualBasic.DateAndTime.DatePart!
| >
| >
| > As to your original question DateTime.DayOfWeek returns the DayOfWeek
Enum
| > with values such as Monday, Tuesday, Wednesday, making
DateTime.DayOfWeek
| > literal which day of the week it is, and not some offset from some
| > arbitrary
| > "FirstDayOfTheWeek". So DateTime.DayOfWeek will return DayOfWeek.Monday
| > when
| > its Monday!
| >
| > Fortunately the DayOfWeek enum values are consecutive, so if you want to
| > convert the DayOfWeek enum to & from an integer value where 0 means
| > Monday,
| > you can use simply comparisons & arithmetic to reimplement the aspect of
| > Microsoft.VisualBasic.DateAndTime.DatePart you are looking for...
| >
| > Something like:
| >
| > static Int16 OffsetFromFirstDayOfWeek(DateTime theDate, DayOfWeek
| > firstDayOfWeek)
| > {
| > Int16 dow = (Int16)((Int16)theDate.DayOfWeek -
| > (Int16)firstDayOfWeek);
| > if (dow < 0)
| > dow += 7;
| > return dow;
| > }
| >
| >
| > DateTime sellDate = DateTime.Now;
| > Int16 dayOfWeek;
| >
| > dayOfWeek = OffsetFromFirstDayOfWeek(sellDate, DayOfWeek.Monday);
| >
| > Alternatively you could use a switch statement if the first day of week
is
| > fixed...
| >
| > Hope this helps
| > Jay
| >
| >
| > | > | Thank you Nicholas.
| > |
| > | Is there any way to do that in C#?
| > |
| > | Regards,L
| > |
| > | "Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
| > in
| > | message | > | > L,
| > | >
| > | > You can still call this. Set a reference to
| > Microsoft.VisualBasic.dll,
| > | > and then call the static DatePart method on the DateAndTime class in
| > the
| > | > Microsoft.VisualBasic namespace.
| > | >
| > | > Hope this helps.
| > | >
| > | >
| > | > --
| > | > - Nicholas Paldino [.NET/C# MVP]
| > | > - (e-mail address removed)
| > | >
| > | > | > | >> dateTime sellDate;
| > | >> int16 dayOfWeek;
| > | >>
| > | >> dayOfWeek=sellDate.DayOfWeek
| > | >>
| > | >> I would like to set that monday is the first day of a week and not
| > | >> sunday.
| > | >> Any idea?
| > | >>
| > | >> In VB there is a function, where you declare which day should be
the
| > | >> first:
| > | >>
| > | >> DatePart(DateInterval.Weekday, sellDate, vbMonday)
| > | >>
| > | >> Regards,
| > | >> L
| > | >>
| > | >>
| > | >
| > | >
| > |
| > |
| >
| >
|
|
 
Hi,

thank you for your answers.
That is what I was looking for.

Have a nice weekend, I'll learn C# :)

Regards,S


Jay B. Harlow said:
Octavio,
You know, in the back of my mind I was thinking modulus, but it just
didn't
click when I was typing...

Thanks for the additional.

Jay

| Jay,
|
| I use more-less the same technique you describe, only I use a modulus
op:
|
| dow = dow % 7;
|
| instead of:
|
| if (dow < 0) dow += 7;
|
| I guess your version performs better...
|
| Regards - Octavio
|
| "Jay B. Harlow [MVP - Outlook]" <[email protected]> escribió en el
| mensaje | > Simon,
| > | Is there any way to do that in C#?
| > Yes, As Nicholas stated: add a reference to the
Microsoft.VisualBasic.dll
| > and call Microsoft.VisualBasic.DateAndTime.DatePart!
| >
| >
| > As to your original question DateTime.DayOfWeek returns the DayOfWeek
Enum
| > with values such as Monday, Tuesday, Wednesday, making
DateTime.DayOfWeek
| > literal which day of the week it is, and not some offset from some
| > arbitrary
| > "FirstDayOfTheWeek". So DateTime.DayOfWeek will return
DayOfWeek.Monday
| > when
| > its Monday!
| >
| > Fortunately the DayOfWeek enum values are consecutive, so if you want
to
| > convert the DayOfWeek enum to & from an integer value where 0 means
| > Monday,
| > you can use simply comparisons & arithmetic to reimplement the aspect
of
| > Microsoft.VisualBasic.DateAndTime.DatePart you are looking for...
| >
| > Something like:
| >
| > static Int16 OffsetFromFirstDayOfWeek(DateTime theDate, DayOfWeek
| > firstDayOfWeek)
| > {
| > Int16 dow = (Int16)((Int16)theDate.DayOfWeek -
| > (Int16)firstDayOfWeek);
| > if (dow < 0)
| > dow += 7;
| > return dow;
| > }
| >
| >
| > DateTime sellDate = DateTime.Now;
| > Int16 dayOfWeek;
| >
| > dayOfWeek = OffsetFromFirstDayOfWeek(sellDate, DayOfWeek.Monday);
| >
| > Alternatively you could use a switch statement if the first day of
week
is
| > fixed...
| >
| > Hope this helps
| > Jay
| >
| >
| > | > | Thank you Nicholas.
| > |
| > | Is there any way to do that in C#?
| > |
| > | Regards,L
| > |
| > | "Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
| > in
| > | message | > | > L,
| > | >
| > | > You can still call this. Set a reference to
| > Microsoft.VisualBasic.dll,
| > | > and then call the static DatePart method on the DateAndTime class
in
| > the
| > | > Microsoft.VisualBasic namespace.
| > | >
| > | > Hope this helps.
| > | >
| > | >
| > | > --
| > | > - Nicholas Paldino [.NET/C# MVP]
| > | > - (e-mail address removed)
| > | >
| > | > | > | >> dateTime sellDate;
| > | >> int16 dayOfWeek;
| > | >>
| > | >> dayOfWeek=sellDate.DayOfWeek
| > | >>
| > | >> I would like to set that monday is the first day of a week and
not
| > | >> sunday.
| > | >> Any idea?
| > | >>
| > | >> In VB there is a function, where you declare which day should be
the
| > | >> first:
| > | >>
| > | >> DatePart(DateInterval.Weekday, sellDate, vbMonday)
| > | >>
| > | >> Regards,
| > | >> L
| > | >>
| > | >>
| > | >
| > | >
| > |
| > |
| >
| >
|
|
 

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

Back
Top