473,787 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17414
I would first get the first day of the year and subtract one, like so:

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

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

DateTime dateTime = lastDayOfPrevio usYear.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.co m

"O.B." <fu******@bells outh.netwrote in message
news:11******** ************@e9 g2000prf.google groups.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.ToShortDateS tring() + "\n" + d.DayOfYear.ToS tring());
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
1680
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 different from solid information. As a result, I have sometimes had to abandon datetime, even though it seemed like the logical choice for representing data. E.g. I might have information like "this paper was published in May 1997". There's no...
7
10860
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 should tell me that today is out of range. In the database we store this as seperate fields: from day integer 1 to 7 from month integer 1 to 12 till day integer 1 to 7
4
2737
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 takes at most 2 arguments (3 given) ******************************** import datetime _datetime = datetime.datetime class MyDateTime(_datetime):
6
6497
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 smalldatetime, which apparently does have that problem of only going as far back as 1900, but since changing to datetime the error still pops up. Is there anything I can do?
6
8991
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 between date and datetime classes? Please, help
4
14372
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
1531
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
14692
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 even 00/00/2008
1
6601
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 '(' UPDATE table SET YEAR(DATE) = 1234 WHERE YEAR(DATE) = 2006;
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.