473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

First Day in the Month

eps
Hi there,

The following code gets the first day in the current month, I don't
understand how it works though, could someone try to explain it to me ?.
DateTime FirstDayInMonth =
DateTime.Now.Subtract(TimeSpan.FromDays(Month.Day - 1));

Any help appreciated.

--
Eps
Nov 27 '07 #1
4 14400
eps
eps wrote:
Hi there,

The following code gets the first day in the current month, I don't
understand how it works though, could someone try to explain it to me ?.

DateTime FirstDayInMonth =
DateTime.Now.Subtract(TimeSpan.FromDays(Month.Day - 1));

Any help appreciated.
sorry the code should be...

DateTime FirstDayInMonth = Month.Subtract(TimeSpan.FromDays(Month.Day - 1));

where month is DateTime.Now

--
Eps
Nov 27 '07 #2
Hello eps,

It works like "substract from today the today - 1" :)

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
eThe following code gets the first day in the current month, I don't
eunderstand how it works though, could someone try to explain it to me
e?.
e>
Nov 27 '07 #3
eps
Michael Nemtsev [MVP] wrote:
Hello eps,

It works like "substract from today the today - 1" :)
---
oh i get it, integer math, I thought it was magic, boy do I feel dense.

--
Eps
Nov 27 '07 #4
Eps,

It should be noted that this code doesn't compile. What you want is:

DateTime FirstDayInMonth =
DateTime.Now.Subtract(TimeSpan.FromDays(DateTime.N ow.Day - 1));

Now, with that, you are going to get the first day of the month, but the
time will be whatever time it is when you run the code. If you want the
beginning of the day, then what you really want to do is this:

DateTime now = DateTime.Now;
DateTime FirstDayInMonth = new DateTime(now.Year, now.Month, 1);

This will give you midnight on the first day of the month.

It also eliminates a subtle bug that existed when you called the static
Now property twice on the DateTime structure. If you ran this around
midnight, you had the chance that the first call to Now would return the day
before midnight, and the second call to Now would result in the day after
midnight, which would give you an incorrect result. You would want to call
Now ^once^, storing the value, and then using that.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"eps" <ep*@mailinator.comwrote in message
news:uD**************@TK2MSFTNGP06.phx.gbl...
Hi there,

The following code gets the first day in the current month, I don't
understand how it works though, could someone try to explain it to me ?.

DateTime FirstDayInMonth =
DateTime.Now.Subtract(TimeSpan.FromDays(Month.Day - 1));

Any help appreciated.

--
Eps
Nov 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: johkar | last post by:
I need to document.write out a select list populated with the dates for the first and third Wednesday of each month. How do I get to the actual days? <select name="mySelect"> <option value="Oct...
6
by: Ante Perkovic | last post by:
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...
2
by: bb | last post by:
any way to prevent displaying the previous and next months in the calendar control? i have tried just blanking the text out in the dayrender IsOtherMonth and also dynamically changing the...
3
by: Sebastian | last post by:
Hello all I have a report where I have two nested groups. I know there are only three standard options for running sum: None, Over Group and Over All. I have a MyTextBox in detail section where...
0
by: larry | last post by:
I am in the process of rewriting one of my first PHP scripts, an event calendar, and wanted to share the code that is the core of the new calendar. My current/previous calendar processed data...
3
by: remya1000 | last post by:
i'm using ASP with MSAccess as database. i have two buttons and two textbox in my page. when i press my first button (First month) i need to display the current month in one textbox and last one...
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
HI I have a stored procedure that returns data with a date field in the form of a DateTime type. I need to place data in variables based on days of the week starting with the first thursday of the...
4
by: Chris Gilpin | last post by:
Hey everybody. I have two tables. The first table lists all of the client ids of the clients I am interested in analyzing. The second table lists various data from their different visits to the...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.