Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date calcution

Status
Not open for further replies.
Apr 11, 2002
193
0
0
IN
Hi,

I want to get the start of current Month, start of week(currect weeks Monday) in mm/dd/yyyy format, start of Quarter. All these dates in mm/dd/yyyy format.

Thanks,
Manish
 
Start of the current month will always be the 1st day.

Start of the week depends on what culture you're in (Muslim cultures use Friday & Saturday as their weekends). Take a look at the Globalization namespace.

Start of Quarter depends on when the fiscal year starts (Some companys, and the US Government, start their year on October 1st). You just need to know this, so make it configurable.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
What have you tried so far?

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Hi,

I tried this for Week to Day.

CultureInfo info = Thread.CurrentThread.CurrentCulture;
string name = CultureInfo.CurrentUICulture.Name;
DayOfWeek firstday = info.DateTimeFormat.FirstDayOfWeek;
DayOfWeek today = info.Calendar.GetDayOfWeek(DateTime.Now);
int diff = today - firstday;
DateTime firstDate = DateTime.Now.AddDays(-diff);

This gives me a start date of a Sunday. I guess en-US has Sunday as the start day of the week, but i want Monday as start date. Do i need to change the cultureInfo here.

Please advice me for Quarter to Day.

Thanks,
Manish
 
If you are always going to use Monday as the week's start date, why not make a function that forces the culture info to US?

I don't know much about the globalization namespace but this works for (whatever my culture settings are, presumably US English)

Code:
            //set a start date to work from
            DateTime myDT = DateTime.Today.AddDays(5);

            //if day of week is sunday we'll subtract 6 days
            int mAdd = 6;
            
            //Otherwise, diff between date and monday
            if (myDT.DayOfWeek != DayOfWeek.Sunday)
            {
                mAdd = (Convert.ToInt16(myDT.DayOfWeek) - Convert.ToInt16(DayOfWeek.Monday));
            }

            //previous monday
            Console.WriteLine("Previous Monday: " + myDT.AddDays(-1 * mAdd));

I would think you can force the culture info to US within your function that returns the date (and not elsewhere), to eliminate any cross-cultural problems... I would alo think someone else will have a better idea for how to do this ;-)

For start of month:
Code:
Console.WriteLine("Start of Month: " + myDT.AddDays(-1 * (myDT.Day - 1)));

For quarter, you will need to know your quarter start dates.

For formatting:
Code:
Console.WriteLine(String.Format("{0:MM/dd/yyyy}", DateTime.Today));

Hope it helps,

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Hi,

The code for Week to day that you gave me returns me with 17th which is the next Monday :).

The Quarter start date will be the 1st of that quarter.

Thanks,
Manish
 
That's because of what it uses for the start date ;)

Code:
DateTime myDT = DateTime.Today.AddDays(5);

Play around with the number of days you add to that.

This code will give you the current quarter's start date (if you are willing to accept SQL Server's definition of a quarter, start dates 1/1, 4/1, 7/1, 10/1)

Code:
            DateTime myQuarterDate;
            myQuarterDate = DateTime.Today.AddMonths(-1 * (DateTime.Today.Month - 1)).AddDays(-1 * (DateTime.Today.Day - 1));
            
            while (myQuarterDate.AddMonths(3) < DateTime.Today)
            {
                myQuarterDate = myQuarterDate.AddMonths(3);
            }

            Console.WriteLine(myQuarterDate);

Hope it help,

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Hi Alex,

Quarter date works perfect. Thanks a lot for the solution.

Manish
 
The others should as well, if you replace myDT with DateTime.Today. THat was only there for testing purposes.

Glad you got it working :)

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top