Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey,

 

Here me is again :o)

 

I want to get the DayOfWeek from the first day of any month....

 

I've got something like this:

        Dim iDowStart As Integer

       iDowStart = DateTime.Parse(Now.Year + "/" + Now.Month + "/01").DayOfWeek

 

This isn't working. Does somebody knows a solution for this problem?

 

[offtopic]When i previewed my post, explorer crashed :confused: [/offtopic]

 

Thanks in advance,

  • *Experts*
Posted (edited)

Whhat do you mean it doesn't work? Do you want it to return a string with the day name instead of a number? Then declare your variable that stores the result as DayOfWeek enum.

   Dim f As DayOfWeek
   f = DateTime.Parse(Now.Year.ToString() & "/" & Now.Month.ToString() & "/01").DayOfWeek
   

Then you can use the ToString method of the DayOfWeek variable to return a string representation of the day.

Edited by mutant
  • *Experts*
Posted

Actually, if you need to worry about globalization - or if you just want to see a full .NET version, try something like this:

DateTime date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
string s = System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames[(int)date.DayOfWeek];

 

My VB version might not be 100% - in fact, I don't know how you cast from one type to another in VB. Below, change the "(int)" in front of "date.DayOfWeek" to however you cast in VB.

Dim date As DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
Dim s As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames((int)date.DayOfWeek)

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Typecasting (int) doen't work here. When i leave it, i will get the name of the day.

 

int(date.DayOfWeek)) also doesn't work...

 

I need the day number.

 

p.s. date is reserved word :-)

Posted

Your original code didn't work, because you were using the plus instead of the ampersand.

 

       Dim iDowStart As Integer
       iDowStart = DateTime.Parse(Now.Year & "/" & Now.Month & "/01").DayOfWeek

 

Note:

Sunday = 0

If you want to be one based then:

 

iDowStart += 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...