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)
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?
I try to convert below text to date, for more than 80 cases from 2011 to 2017-July it is working fine but from Aug it converts the year to the year 2018 and considers the number as a day. I want to check is there any way to convert format date exactly and properly? this piece of code is for data migration so if it work wrongly it is disaster situation.
Convert.ToDateTime("Sep-17")
The answer in my machine is: {9/17/2018 12:00:00 AM}
the correct answer should be: {9/1/2017 12:00:00 AM}
DateTime.ParseExact("Sep-17", "MMM-yy", CultureInfo.InvariantCulture);
I have a drop down menu containing several different dates, where one will be selected based in if it matches a date in an Excel sheet I'm using. The dates in the dropdown menu are of the format Wednesday, August 9, 2016.
Now my plan was to convert the Excel data to a long date format and compare the two; however, I noticed that C#'s long date format includes a zero preceding the day, like Wednesday, August 09, 2016.
Is there a way that I can remove that extra 0 in front of the 9 when converting to a long date format in C#? Or is there another method I should try.
This should do:
mydate.ToString("dddd, MMMM d, yyyy", CultureInfo.CreateSpecificCulture("en-US"));
If your format is fixed that way, you can just use a custom format string:
date.ToString("dddd, MMMM d, yyyy", CultureInfo.GetCultureInfo("en-us"));
this format can do the job:
String date = DateTime.Now.ToString("dddd,MMMM,d,yyyy",CultureInfo.GetCultureInfo("en-us"));
// Wednesday, August 9, 2016
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;