Windows form setting datetimepicker to 31st march of every year - c#

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
Right now the code display current date in the date time picker.
How do I display 31st March in 2017 and once it is 2018, it will display 31st March 2018 and so on and so forth as the year progress
Anyone help would be greatly appreciated

int currentYear = DateTime.Today.Year;
DateTime desiredDate = new DateTime(currentYear, 3, 31);
Additional info: apparently Asker doesn't know class DateTime
You asked:
How do I display 31st March in 2017 and once it is 2018, it will
display 31st March 2018 and so on and so forth as the year progress
This is a difficult way to say that you want 31 march of the current year (= the year of Today)
DateTime.Today fetches the date of Today (surprise, surprise!)
Property Year contains the Year of Today
On 15th November 2017, Year was 2017
new DateTime(currentYear, 3, 31) will make the date of 31st March of the current year (which in my example was 2017
Surely you did read the documentation of the DateTime structure, didn't you?

Related

How to get 29 Feb using AddYears method in c#

We use a job which runs every day and perform some action for a day one year ahead.
Actually we just use something like: DateTime.UtcNow.AddYears(1).
But it seems not possible to get a 29 February(e.g for 2020) using this technique:
var target = new DateTime(2020, 2, 29);
bool result = (target == target.AddYears(-1).AddYears(1));//false
So is it possible to target a 29 February in future somehow?
No. The Documentation states:
If value + DateTime.Year is also a leap year, the return value represents the leap day in that year. For example, if four years is added to February 29, 2012, the date returned is February 29, 2016.
If value + DateTime.Year is not a leap year, the return value represents the day before the leap day in that year. For example, if one year is added to February 29, 2012, the date returned is February 28, 2013.
This means if you add a year you will always get Feb 28th. The only way to get 29th via AddYears is if you add a multiple of 4.
No, this is by design.
If the current instance represents the leap day in a leap year, the
return value depends on the target date:
If value + DateTime.Year is also a leap year, the return value
represents the leap day in that year. For example, if four years is
added to February 29, 2012, the date returned is February 29, 2016.
If value + DateTime.Year is not a leap year, the return value
represents the day before the leap day in that year. For example, if
one year is added to February 29, 2012, the date returned is February
28, 2013.
1 year after 29th Feb 2020 should be 28th Feb 2021 since it is not a leap year. But in such a case, all the years after 2021 will work as a 28th February.
Other than this, ask yourself, what is the meaning of a "year" for you? How many days in a month? How many days in a year? Is it 365? 366? Or as wikipedia stated 365.2425? Also, which calendar we are talking about?
Frameworks, libraries etc.. does not work like people think. They work based on a set of rules that defined before. .NET Framework defined this rule as such. So, when you add a year to a DateTime instance, what they decide is month part has to stay same, year part will change for how many years will be added, and the day part must be a valid one.
You could cheat the system a bit by taking "the day before march 1st" instead.
stub:
DateTime today = DateTime.UtcNow
if (today.month == 2)
{
if(today.day == 28 || today.day == 29)
{
today.AddDays(1).AddYears(1).AddDays(-1)
}
}
this converts your feb 28 on non-leapyears in march 1st, adds a year, and goes to the day before that.
If the next year also is not a leapyear, you will still get feb 28, but if next year is a leapyear, the result will be feb 29.
this does not deal yet with the situation that this year is a leapyear, as this code will then return febuary 27th instead though
Function that return the next 29. feb. Maybe it helps.
using System;
public class Program
{
public static DateTime NextTwentyNineFeb()
{
int year = DateTime.Now.Year;
while(true){
try{
var target = new DateTime(year, 2, 29);
Console.WriteLine(target);
return target;
}
catch
{
year++;
}
}
}
}
As NibblyPig stated, this isn't possible. However, if you are actually just looking for the end of the month, then you can use new DateTime(year, month + 1, 0).AddDays(-1)

Datetime get time of the next day at exactly 9 AM by the selected time zone

I have a code like this:
var info = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var localServerTime = DateTimeOffset.Now;
DateTimeOffset localTime = TimeZoneInfo.ConvertTime(localServerTime, info);
This gets me a desired "CEST" current time regardless of the server location. Now I would like to extract from this variable date that is set in future.
For example, today is 2nd of July 2018 11:24 AM by my local time. The "setInFuture" DateTime should be set to 3rd of July 2018 9:10 AM
Another example:
14th of July 2018 11:24 AM by my local time. The "setInFuture" DateTime should be set to 15th of July 2018 9:10 AM
So the set in future variable should always be next day of CEST current time at 9:10 AM...
How can I achieve this, regardless of what current time it is at CEST?
Can someone help me out?

Date difference from Noda Time is correct?

DateTime dtStart = new DateTime(2015,7,28);
LocalDate ldtStart = LocalDate.FromDateTime(dtStart);
DateTime dtEnd = new DateTime(2017, 2, 1);
LocalDate ldtEnd = LocalDate.FromDateTime(dtEnd);
Period period = Period.Between(ldtStart, ldtEnd, PeriodUnits.YearMonthDay);
Result for above:
period.Years -> 1
period.Months -> 6
period.Days -> 4
As you can see the difference i got from Noda Time library.
But i get different result for https://www.easycalculation.com/date-day/age-calculator.php
Result for above link:
1 years, 6 months, and 1 days
Start Date: 28th July 2015
End Date: 1st Feb 2017
Can someone please tell me that the result i got from noda time plugin is more accurate then the link I provided?
"More accurate" requires a specification of how you want to compute the difference. There's no single right answer here. As documented, Noda Time works element-wise. So if you add 1 year, 6 months and 4 days to 28th July 2015 you get:
Adding 1 year: 28th July 2016
Adding 6 months: 28th January 2017
Adding 4 days: 1st February 2017
The code used for the site is available on the site itself. It looks like that's taking a rather more naïve approach.
In particular, if you ask it how old someone born on January 31st 2017 is on February 1st 2017, they'll say they're -2 days old. I don't think that's right...
From 28th July 2015 to 1st August 2015 is 4 days and from 1st August 2015 to 1st Feb 2017 is exactly one and a half year.
NodaTime shows you correct information. You also could check with this link.
You could try to change in your link date from 28th July 2015 to 29th July 2015 or 30th July 2015 and you will see invalid input.

Remove time zone offset from date in C#.Net

i am getting data in xml format which contains below fields
Sat Feb 13 2016 01:59:28 GMT+1100 (AUS Eastern Standard Time)
The value of ActivatedDate=Sat Feb 13 2016 01:59:28 GMT+1100 (AUS Eastern Standard Time)
i want to get the date and time from the ActivatedDate and compare with today's date and find the difference in time span(hour) of both the dates .
For ex: (Sat Feb 13 2016 01:59:28 - 22nd Feb 2016).Hour
I want to find the difference between two dates with respect to hours .
Please help me on this.
This is assuming that you need to parse the ActivatedDate from a string:
string activatedDate = "Sat Feb 13 2016 01:59:28";
DateTime activeDate = DateTime.Parse(activatedDate);
TimeSpan timeDifference = DateTime.Now.Subtract(activeDate);
double hoursDifference = timeDifference.TotalHours;

Get full date when DateTime.DayOfYear and Year is given

Is there a possibility to get the full date out of the year and the dayOfYear value?
For example: today is Thursday, 19th of february 2015.
The dayOfYear-Value of today is 50.
If I have the value 75 and the year 2010, how am I able to get the matching date?
It could be displayed in a textBox, dateTimePicker, whatever.
But you only have the information year & dayOfYear.
Thanks
You can use the following code:
DateTime day = New DateTime(2010, 1, 1).AddDays(75 - 1);
First get the first day of the year, then add necessary day count minus one (you are already on the first date of the year) days to the first day and you are there.

Categories

Resources