Subtracting TimeSpan from date - c#

I want to subtract a time-span from a date-time object using a 30-day month and ignoring leap years etc.
Date is 1983/5/1 13:0:0 (y/m/d-h:m:s)
Time span is 2/4/28-2:51:0 (y/m/d-h:m:s)
I can use DateTime and TimeSpan objects to do this, after converting years and months of the time-span to days (assuming a 30 day month and a ~364 day year).
new DateTime(1981,5,1,13,0,0).Subtract(new TimeSpan(878,13,51,0));
With this i get the result:
{12/4/1978 11:09:00 PM}
Above answer obviously doesn't ignore the factors i want ignored and gives me an accurate answer. But in this case that's not what i want so i wrote the below code.
public static CustomDateTime operator -(CustomDateTime DT1,CustomDateTime DT2)
{
CustomDateTime retVal = new CustomDateTime();
try
{
const int daysPerYear = 364.25;
const int monthsPerYear = 12;
const int daysPerMonth = 30;
const int hoursPerDay = 24;
const int minutesPerHour = 60;
retVal.Minute = DT1.Minute - DT2.Minute;
if (retVal.Minute < 0)
{
retVal.Minute += minutesPerHour;
DT1.Hour -= 1;
}
retVal.Hour = DT1.Hour - DT2.Hour;
if (retVal.Hour < 0)
{
retVal.Hour += hoursPerDay;
DT1.Day -= 1;
}
retVal.Day = DT1.Day - DT2.Day;
if (retVal.Day < 0)
{
retVal.Day += daysPerMonth;
DT1.Month -= 1;
}
retVal.Month = DT1.Month - DT2.Month;
if (retVal.Month < 0)
{
retVal.Month += monthsPerYear;
DT1.Year -= 1;
}
retVal.Year = DT1.Year - DT2.Year;
}
catch (Exception ex) { }
return retVal;
}
Then i get:
1981/0/3-10:9:0
This is pretty close to what i'm after except i shouldn't get 0 for month and year should be 1980. Any kind of help is appreciated.
Just to make things clear again; in this context I have to use a 30-day month and ignore leap-years, different numbers of months, etc. Its a weird thing to do, i know. So I'm pretty much after a 'wrong answer' as opposed to the exact answer given by the managed classes.

If you're estimating a month at 30 days, of course your math will be off. When you subtract 878 days from 5/1/1981, .Net is giving you the exact difference, not an estimate, and this difference accounts for leap years, if there are any. The error is not in the Subtract(...) method - it is in your own "manual" calculation.
DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0);
TimeSpan t = new TimeSpan(878, 13, 51, 0);
dt.Ticks
624931668000000000
t.Ticks
759090600000000
dt.Ticks - t.Ticks
624172577400000000
new DateTime(dt2)
{12/4/1978 11:09:00 PM}
Date: {12/4/1978 12:00:00 AM}
Day: 4
DayOfWeek: Monday
DayOfYear: 338
Hour: 23
Kind: Unspecified
Millisecond: 0
Minute: 9
Month: 12
Second: 0
Ticks: 624172577400000000
TimeOfDay: {23:09:00}
Year: 1978
These are the total ticks since the epoch. Do this math, then convert back into a datetime.
Also: correct your math. 878 days is 2 years and 148 days. 5/1/1981 is the 121st day of the year, so subtract 120 to get Jan 1, 1979. This leaves 28 days. Start counting backwards from the end of 1978, and you get very close to the .Net answer. Your own answer isn't anywhere close.
EDIT based on feedback
// zh-Hans is a chinese culture
CultureInfo ci = CultureInfo.GetCultureInfo("zh-Hans");
DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0, ci.Calendar);
TimeSpan t = new TimeSpan(878, 13, 51, 0);
Please note that you are still subtracting 878 days. The length of a month would be irrelevant in that case based on the Julian calendar. You will probably need to find the correct culture code for your particular calendar, then try this. However, with this calendar, I still arrive at the same answer above.
Beyond doing this, I am unsure how else to do the math. If you can provide a link to how you are doing it by hand, I can help code it for you.
EDIT 2
I understand now. Try this:
DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0, ci.Calendar);
int years = 878 / 365;
int remainingDays = 878 % 365;
int months = remainingDays / 30;
remainingDays = remainingDays % 30;
TimeSpan t = new TimeSpan(years * 365 + months * 30 + remainingDays);
DateTime newdate = dt.Subtract(t);

You cannot assume a 30-day month. You are specifying that you want to subtract 878 days. The managed classes (I'm assuming you mean managed when you say native) are designed to factor in leap-years, different numbers of months, etc.
Using the managed classes will not give you a 0 for a month.

Related

Return the correct number of weeks for the Gregorian Calendar

I have a problem with the calendar that I use to get me a smaller number of weeks for certain months
For example, this happens to me at Sept 2019, where my number is 5 or in July 2018, which is also 5.
How can I fix this?
this is my current code:
private DateTime _calendarDate;
int numWeeks = NumberOfWeeks(_calendarDate.Year, _calendarDate.Month);
private int NumberOfWeeks(int year, int month)
{
return NumberOfWeeks(new DateTime(year, month, DateTime.DaysInMonth(year, month)));
}
private int NumberOfWeeks(DateTime date)
{
var beginningOfMonth = new DateTime(date.Year, date.Month, 1);
while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
date = date.AddDays(1);
return (int)Math.Truncate(date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;
}
additional information on CultureInfo
The problem is that it always comes back to me one week less, so my calendar doesn't display it properly and then I get the error,
Here's an example for April 2018 where you give me 5 weeks, and then another one is missing and that's why I'm getting the error
can anyone guess how i could solve this problem?
When you can use System.Globalization.Calendar you can get the count of weeks in a month by using GetWeekOfYear for the first and the last day of the month and then calculate the difference (and add 1 to include the first week).
This would change your NumberOfWeeks() to the following:
private int NumberOfWeeks(DateTime date)
{
Calendar calendar = CultureInfo.CurrentCulture.Calendar;
var firstOfMonth = new DateTime(date.Year, date.Month, 1);
var week1 = calendar.GetWeekOfYear(firstOfMonth, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
var week2 = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
int numberOfWeeks = (week2 - week1) + 1;
return numberOfWeeks;
}
For your example (April 2018) this will give you 6 as a result and for May 2018 it will give you 5.
MSDN GetWeekOfYear
Here is a bit more mathematical solution:
int myYear = 2019, myMonth = 12; // the example for December 2019
var firstMonthDay = new DateTime(myYear, myMonth, 1);
int delta = firstMonthDay.DayOfWeek - System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
if (delta < 0)
delta += 7;
int daysInMonth = DateTime.DaysInMonth(myYear, myMonth);
int weekLinesNumber = (int)Math.Ceiling((daysInMonth + delta) / 7F); // the result
We count the delta (could be from 0 to 6), this is the number of days from the first day of the week to the first next month day. Then we count the resulting number of the weeks using Math.Ceiling function.
In short, we add to the month some delta days at the beginning in order to get new Extra Month block starting from the First Day of a Week (according to the culture). And then we count the total number of the occupied week lines. No need to add +1 when we use Math.Ceiling function.

How to calculate amount of hours between two points in c#/Code review

Hello everyone I have a small programming problem which is probably a lot easier than i think. So I need to set the time to install Timespan opbject below to be 24 + time left to the next 4 pm. The below is C# pseudo code, it was written in notepad because at work I don't have an IDE, i also don't have much experience in programming using dates. i think my alghorithm will work but i guess there is a milion easier ways to do it. Please have a look:
//I need to make a timespan object which has 24 hours from current time + time left to the next 4pm
//The context is time to install, which user should see
Timespan TimeToInstall = new Timespan(23,59,59)
//Now I am taking the current time
Current = DateTime.Now
//Now I add the 24 hours to the current in order to create the next day date
Current.Add(TimeToInstall)
//Now creating the 4 PM on the next day
DateTime pm4 = new DateTime(Current.year,Current.month,Current.Day,16,0,0)
//Now checking if current is above or below 4 pm
if(Current.TimeOfDay < pm4){
TimeToInstall = TimeToInstall + (pm4 - Current)
}else if(Current.TimeOfDay > pm4){
pm4.AddDays(1)
TimeToInstall = TimeToInstall + (pm4 - Current)
}else {
//24 hours has passed and it is 4 pm so nothing to do here
}
TimeSpan can be negative. So just substract the TimeSpan for 4PM with current TimeOfDay, if you get negative value, add 24 hours.
var timeLeft = new TimeSpan(16, 0, 0) - DateTime.Now.TimeOfDay;
if (timeLeft.Ticks<0)
{
timeLeft = timeLeft.Add(new TimeSpan(24,0,0))
}
Based on your code:
DateTime now = DateTime.Now;
DateTime today4pmDateTime= new DateTime(now.Year, now.Month, now.Day, 16, 0, 0);
//Will hold the next 4pm DateTime.
DateTime next4pmDateTimeOccurrence = now.Hour >= 16 ? today4pmDateTime : today4pmDateTime.AddDays(1);
//From here you can do all the calculations you need
TimeSpan timeUntilNext4pm = next4pmDateTimeOccurrence - now;
The answer is really simple, I should have seened this earlier. The solution to these kind of problems is basically modular arithmetic. The client requirment was the popup to show 24+ time to next 4 pm (Don't ask i don't know) so if:
program runs at 13:00 then the clock should display 24 +3 = 27
when 16:00 it should be 24+24 which is 48
when 22:00 it shoould be 24 + 18 which is 42
Now I noticed that:
13 + 27 = 40
16 + 24 = 40
22 + 18 = 40
40 Modulo 24 = 16
So basically if I subtract the current time from 40 then I will be left with the difference:
40 - 13 = 27
40 - 16 = 24
40 - 22 = 18
So what I did is this:
TimeSpan TimeToInstall;
TimeSpan TimeGiven = new TimeSpan(23, 59, 59);
DateTime Now = DateTime.Now;
long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks) + TimeGiven.Ticks;
TimeToInstall = TimeSpan.FromTicks(TimeTo4);
EDIT
The above was a trap
Corrected:
DateTime Now = DateTime.Now;
if (Now.Hour < 16)
{
long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks);
TimeToInstall = TimeSpan.FromTicks(TimeTo4);
}
else
{
long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks) + TimeGiven.Ticks;
TimeToInstall = TimeSpan.FromTicks(TimeTo4);
}

Convert week and year to milliseconds

I need to convert, as the title says, weeks and year to milliseconds since 1970. What is the best way to do this in .Net?
I only have information of the week and year that some event occurred. The week stars on Monday. I think DateTime is not the answer since it can't handle the week of the year.
What I need is something like a method double getMili(int week, int year).
Thanks anyway
You can put it like that:
// Let's convert 15th Monday in 2014
int MondaysNumber = 15;
DateTime source = new DateTime(2014, 1, 1);
int delta = 7 + DayOfWeek.Monday - source.DayOfWeek;
if (delta >= 7)
delta -= 7;
source = source.AddDays((MondaysNumber - 1) * 7 + delta);
// Finally, convert it into milliseconds
Double result = (source - new DateTime(1970, 1, 1)).TotalMilliseconds;

Errornous calculation of year/month/day between two dates

What I try to do is calculate the number of years, months and days between two dates.
Unfortunately, there is no method of the .NET Framework which can do this.
What I did is basically the following:
http://www.codeproject.com/Articles/28837/Calculating-Duration-Between-Two-Dates-in-Years-Mo
with a few adaptations from the comments of said website:
var monthDay = new[] { 31, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
DayCalculation:
if (fromDate.Day > toDate.Day)
{
increment = monthDay[toDate.Month - 1];
}
if (increment == -1)
{
increment = DateTime.IsLeapYear(toDate.Year) ? 29 : 28;
}
So I have the following effect:
Date1: 1979-01-30
Date2: 2013-03-01
And the output is: 34 Years, 1 Month, -1 Day
The expected output is: 34 Years, 1 Month, 1 Day
This effect happens every time, the Date2 is a date in march.
Do you know what is wrong with this calculation?
Or do you know a better solution how to achieve the desired result?
Thanks in advance
PS: I know that you can calculate the amount of days between two dates, but what I need is the amount of finished years, the amount of finished months, and the amount of finished days
Unfortunately, there is no method of the .NET Framework which can do this.
True, but there is Noda Time instead :) (It's my port of Joda Time to .NET, although there are quite a few differences between the two projects.)
LocalDate start = new LocalDate(1979, 1, 30);
LocalDate end = new LocalDate(2013, 3, 1);
Period period = Period.Between(start, end);
Console.WriteLine("{0} years, {1} months, {2} days",
period.Years, period.Months, period.Days);
Output:
34 years, 1 months, 1 days
Here is a way to calculate the difference without using an external library. Two dates are required. I assume that the first date is no later than the second date. Otherwise you will have to swap them for the calculation to be correct.
var first = new DateTime(1979, 1, 30);
var second = new DateTime(2013, 3, 1);
Here is how to compute the difference. You do not need a table to get the number of days in the month. That information is provided by the DateTime.DaysInMonth function.
var years = second.Year - first.Year;
var months = second.Month - first.Month;
if (months < 0) {
months += 12;
years -= 1;
}
var days = second.Day - first.Day;
if (days < 0) {
var daysInFirstMonth = DateTime.DaysInMonth(first.Year, first.Month);
days += daysInFirstMonth;
months -= 1;
}
Printing the computed values
Console.WriteLine("{0} year(s), {1} month(s), {2} day(s)", years, months, days);
will result in
34 year(s), 1 month(s), 2 day(s)
This produces the same results as the code that you have linked to. However, you expect to get 1 day instead of 2 days. I guess it depends on how you define "days between". If you prefer to count only January 31 as the day between you can subtract 1 from days. Then there will be 0 days between two adjacent days and -1 days "between the same date".

How to convert year, month and day to ticks without using DateTime

long ticks = new DateTime(2012, 1, 31).ToLocalTime().Ticks; // 634635684000000000
But how to do this without DateTime constructor ?
edit
What I actually want is to keep only the years, months and days from the ticks.
long ALL_Ticks = DateTime.Now.Ticks; // 634636033446495283
long Only_YearMonthDay = 634635684000000000; // how to do this ?
I want to use this in a linq-sql query using Linq.Translations.
If you only want the ticks for the date portion of the current datetime you could use:
long Only_YearMonthDay = DateTime.Now.Date.Ticks; //634635648000000000
//DateTime.Now.Date.Ticks + DateTime.Now.TimeOfDay.Ticks == DateTime.Now.Ticks
You could find out how many days are in the calculation and then multiply by 864,000,000,000 (which is how many ticks are in a day). Is that what you are looking for? Bit of documentation here : http://msdn.microsoft.com/en-us/library/system.timespan.ticksperday.aspx.
Happy coding,
Cheers,
Chris.
OK - didn't think this through properly! Ticks represent the amount of 100 nanosecond intervals since 12:00:00 midnight, January 1, 0001. You would need to calculate how many days have passed since that date and then multiply it by the ticks per day value!
If I understand you right, you are not worried about the ticks up to a particular time of the day?! So, it would be something along the lines of :
var ticksToDate = (DateTime.UtcNow - DateTime.MinValue).Days * 864000000000;
Does that answer your question??
That is going to be rather difficult unless you have some other way of getting the current date and time. According to MSDN:
A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.
Now, if you know the current date and time, you can calculate how many days have passed since January 1, 0001 and use that to calculate the number of ticks.
I understand you dont want the hour parts of the date. If you use Date, then you only get the day (for example: 01/01/2012 00:00:00)
long ticks = new DateTime(2012, 1, 31).Date.Ticks;
And with any DateTime object already created is the same of course.
long ticks = dateObject.Date.Ticks;
You already have the answer there in your post:
long ALL_Ticks = DateTime.Now.Ticks;
// that's the ticks (from DateTime.MinValue) until 'now' (this very moment)
long ticks = new DateTime(2012, 1, 31).ToLocalTime().Ticks;
// or
long ticks = DateTime.Now.Date.Ticks;
// that's the ticks until the beginning of today
long yearmonthticks = new DateTime(2012, 1, 1).ToLocalTime().Ticks;
// that's the ticks until the beginning of the month
// etc..., the rest is simple subtractions
Since your question doesn't specify any reason not to use the DateTime constructor, this is the best solution for what seems like your problem.
I had a use case where I couldn't use DateTime but needed Years/Months from Ticks.
I used the source behind DateTime to figure out how. To go the other way you can look at the constructor, one of which calls the following code.
private static long DateToTicks(int year, int month, int day) {
if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) {
int[] days = IsLeapYear(year)? DaysToMonth366: DaysToMonth365;
if (day >= 1 && day <= days[month] - days[month - 1]) {
int y = year - 1;
int n = y * 365 + y / 4 - y / 100 + y / 400 + days[month - 1] + day - 1;
return n * TicksPerDay;
}
}
throw new ArgumentOutOfRangeException(null, Environment.GetResourceString("ArgumentOutOfRange_BadYearMonthDay"));
}
This can be found in link below, of course you will need to re-write to suit your needs and look up the constants and IsLeapYear function too.
https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,602

Categories