473,408 Members | 1,769 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,408 software developers and data experts.

DateTime with only Year and Day of Year

Does C# provide an operation for converting year and day-of-the-year
(1-365) to a DateTime object?

Oct 17 '07 #1
2 17360
I would first get the first day of the year and subtract one, like so:

DateTime lastDayOfPreviousYear = (new DateTime(year, 1, 1)).AddDays(-1);

Then, you can add the number of days to the lastDayOfPreviousYear and it
will give you the appropriate DateTime:

DateTime dateTime = lastDayOfPreviousYear.AddDays(days);

Of course, you can just get the first date of the year, and subtract one
from the number of days, like so:

DateTime firstDayOfYear = new DateTime(year, 1, 1);
DateTime dateTime = firstDayOfYear.AddDays(days - 1);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"O.B." <fu******@bellsouth.netwrote in message
news:11********************@e9g2000prf.googlegroup s.com...
Does C# provide an operation for converting year and day-of-the-year
(1-365) to a DateTime object?

Oct 17 '07 #2
O.B. wrote:
Does C# provide an operation for converting year and day-of-the-year
(1-365) to a DateTime object?
No, but you could create a DateTime for your year, and add the number of
days to it like:

int year = 2007;
int day = 186;

DateTime d = new DateTime(year, 1, 1).AddDays(day - 1);

MessageBox.Show(d.ToShortDateString() + "\n" + d.DayOfYear.ToString());
The subtraction is necessary or you'll end up on day 187.

Chris.
Oct 17 '07 #3

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

Similar topics

6
by: Terry Hancock | last post by:
What do you do when a date or time is incompletely specified? ISTM, that as it is, there is no formal way to store this --- you have to guess, and there's no way to indicate that the guess is...
7
by: Eddy | last post by:
I have to check whether a given date is between a day and a month. A guided tour is only scheduled from november 1st until april 1st. when i want to make a reservation for the tour Today the query...
4
by: Rob Conner | last post by:
So this is simple, why can't I run the following code? I've tried many variances of this, but simply cannot inherit from datetime or datetime.datetime. I get this on line 3. TypeError: function...
6
by: Not Me | last post by:
Hi, I'm getting the datetime field overflow error using odbc to connect to an sql database from access. This happens when I try to enter the date 13/01/1899. It did happen when using...
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...
4
slapshock
by: slapshock | last post by:
Can anyone help me on how to get only year in a date... e.g. 09/15/1986 i want to get only the 1986....please give me some idea on how to do this.... thanks in advance...
3
by: shapper | last post by:
Hello, I am checking a DateTime variable to check if in range: low < MyDateTime < high. However, I know need do the same but not care about the year. Can I do this? Thanks, Miguel
10
by: sparks | last post by:
We are entering mm/dd/yyyy but some of the date fields only contain years. Can a date be made to only take a year entry if that is all that they have? can it be made to take only 2008 or maybe...
1
by: amahajan4 | last post by:
I want to change the Year in Date . For example 01.02.0009 into 01.02.2009 Please suggest how can I do it in DB2...? I tried the below code but it is giving Illegal symbol error for '(' ...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.