Add decimal number to Date - C# - c#

How to convert a decimal number (e.g. 2.5) to year and month (2 years and 6 months) and add it to a given date? I tried DateTime.TryParse and it didn't work.

If you are using it for years then multiply the float you have by 12. 2.5 becomes 30months. Then use the addmonths function. If I enter 5 then it will add 60 months which is 5 years

Usually you could just add a TimeSpan or use one of the Add methods, like this:
decimal yearsToAdd = (decimal)2.5;
int years = (int)Math.Floor(yearsToAdd);
decimal months = yearsToAdd - years;
int actualMonths = (int) Math.Floor(months * 12); // or Ceiling or Round
DateTime x = DateTime.Now.AddYears(years).AddMonths(actualMonths);
The problem is, that when you decimal doesn't yield an exacat number of months, how would you know how long e.g. half a month is?
28.0 / 2, 29.0 / 2, 30.0 / 2 or 31.0 / 2?
Would you take the length of the month you started with or one of the possible two months you end up with?

If you init date is dt than
dt = dt.AddMonths((int)(2.5*12));

decimal x =(decimal)2.5;
int nbYear = Convert.ToInt16(x);
var y = x - Math.Truncate(x);
int nbMonth =Convert.ToInt16 (y*12);
// MessageBox .Show (string.Format (" {0} years and {1} months ",nbYear ,nbMonth ));
DateTime dat=DateTime .Now ; // or given date
DateTime dat2 = dat.AddYears(nbYear).AddMonths(nbMonth);

If month is your smallest unit then the solution is, as pointed by many, to multiply number by 12. A more accurate alternative would be to use ticks.
decimal years=3.14592M; // No idea where this came from.
long ticks = (long)(356.0M * (decimal)TimeSpan.TicksPerDay * years);
DateTime futureDate=DateTime.Today.AddTicks(ticks);
Note that solution will not compensate for leap years. It is not difficult to extend it - you need to calculate number of leap years in the period and use average instead of 356.0M to calculate ticks per year (i.e. avg. number of days per year * ticks per day).

Related

How to get time difference of two values taken by Winforms DateTimePicker only considering hours minutes and seconds and without miliseconds in C#? [duplicate]

This question already has answers here:
Can you round a .NET TimeSpan object?
(10 answers)
How can I round up the time to the nearest X minutes?
(15 answers)
Closed 10 months ago.
I'm getting values from two dateTimePickers and I need to get the time difference between them. Now, I'm calculating the difference using the below code, but I'm getting a wrong time difference with Milliseconds. Is there a way to calculate the time difference without considering Milliseconds.
WindowsFormScreenshot
TimeSpan difference = dateTimePicker2.Value - dateTimePicker1.Value;
Example 1;
Start time : 4/24/2022 2:30:11 PM
end time : 4/24/2022 2:30:11 PM
Actual difference : -00:00:00.0049863
Expected difference : 00:00:00
Example 2;
Start time : 4/24/2022 2:29:01 PM
end time : 4/24/2022 10:29:01 PM
Actual difference : 07:59:59.9037925
Expected difference : 08:00:00
Example 3;
Start time : 4/24/2022 4:24:28 PM
end time : 4/24/2022 10:30:28 PM
Actual difference : 06:05:59.9648821
Expected difference : 06:06:00
It will give expected answers, when I'm retyping the time value in both dateTimePickers in Winform but doesn't work when I didn't retype minutes field in each dateTimePicker.
I don't want to round up the answers. I want to do the difference calculation without considering milliseconds. So that it will give correct answers for what ever the values we parse for dateTimePickers.
(I'm a newbie to c#. so I'm sorry if I'm not much clear. Thank you so much!)
DateTime adjustedDateTimePicker2 = dateTimePicker2.Value.AddMilliseconds(dateTimePicker2.Value.Millisecond * -1);
DateTime adjustedDateTimePicker1 = DateTimePicker.Value.AddMilliseconds(dateTimePicker.Value.Millisecond * -1);
TimeSpan difference = adjustedDateTimePicker2 - adjustedDateTimePicker1;
You may read hours, minutes, and seconds separately:
int hourCnt = difference.Hours;
int minuteCnt = diffenrence.Minutes;
int secondCnt = difference.Seconds;
You can first round the timespan, then format the result using a Custom Timespan format string like "hh':'mm':'ss" or #"hh\:mm\:ss":
var d1 = DateTime.Parse("4/24/2022 4:24:28 PM");
var d2 = DateTime.Parse("4/24/2022 10:30:28 PM");
var d = d2 - d1;
int precision = 0;
const int TIMESPAN_SIZE = 7;
int factor = (int)Math.Pow(10, (TIMESPAN_SIZE - precision));
TimeSpan roundedTimeSpan =
new TimeSpan(((long)Math.Round((1.0 * d.Ticks / factor)) * factor));
MessageBox.Show(roundedTimeSpan.ToString(#"hh':'mm':'ss"));
Which shows:
06:06:00

How to get the total hours with different dates in C#

This is my problem now, i want to get the total hours and mins of work.
example from jan. 11 2017 22:00 to Jan. 12 2017 7:00.
so far i have it only work when the end date is not change
DateTime pin = today, pout = DateTime.Parse(empTime);
TimeSpan spanMe = pout.Subtract(pin);
spanMe.Hours
spanMe.Minutes
it gives me negative numbers.
it gives me negative numbers.
That is expected if you subtract a larger item from a smaller item (ie. subtracting a more recent time from an older time). If you always want to see the difference as a positive number and do not want to take into account which is larger then wrap the result of the properties (like .Hours) in Math.Abs (absolute value).
var hours = System.Math.Abs(spanMe.Hours);
var minutes = System.Math.Abs(spanMe.Minutes);
Also as pointed out by #stuartd there is a difference between Hours/Minutes and TotalHours/TotalMinutes. Make sure you are using the correct one for your needs.
It should work :
DateTime pin = DateTime.Parse("jan 11 2017 22:00");
DateTime pout = DateTime.Parse("Jan 12 2017 7:00");
TimeSpan spanMe = pout.Subtract(pin);
Console.WriteLine("Hours : {0}, Minutes : {1}", spanMe.Hours, spanMe.Minutes);
Console.ReadLine();
if you know what is the latest date, you need arrange it accordingly. If not, you can not multiply by -1:
double GetHouers(DateTime one, DateTime another)
{
var diff = (one - another).TotalHours;
return diff > 0 ? diff : diff * -1;
}
You can subtract one DateTime Object from another, and then use .TotalHours property of the DateTime class to get the number of hours. It will give you a double value representing the total hours.
DateTime pin = today, pout = DateTime.Parse(empTime);
double hours = (pout - pin).TotalHours;
DateTime pin = today, pout = DateTime.Parse(empTime);
TimeSpan spanMe = pin.Subtract(pout);
var hours = spanMe.TotalHours;
var minutes = spanMe.TotalMinutes;
You want to use TotalHours and TotalMinutes as these will handle fractions thereof, versus Hours and Minutes which return only whole values. You also need to swap the order of your operands as above for the subtraction step.

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

TimeSpan for different years subtracted from a bigger TimeSpan

The language I am using is C#.
I have the folowing dillema.
DateTime A, DateTime B. If A < B then I have to calculate the number of days per year in that timespan and multiply it by a coeficient that corresponds to that year.
My problem is the fact that it can span multiple years.
For example:
Nr of Days in TimeSpan for 2009 * coef for 2009 + Nr of Days in TimeSpan for 2010 * coef for 2010 + etc
You can't do this with a simple TimeSpan, basically. It doesn't know anything about when the span covers - it's just a number of ticks, really.
It sounds to me like there are two cases you need to consider:
A and B are in the same year. This is easy - just subtract one from the other and get the number of days from that
A and B are in different years. There are now either two or three cases:
The number of days after A in A's year. You can work this out by constructing January 1st in the following year, then subtracting A
The number of days in each year completely between A and B (so if A is in 2008 and B is in 2011, this would be 2009 and 2010)
The number of days in B's year. You can work this out by constructing December 31st in the previous year, then subtracting B. (Or possibly January 1st in B's year, depending on whether you want to count the day B is on or not.)
You can use DateTime.IsLeapYear to determine whether any particular year has 365 or 366 days in it. (I assume you're only using a Gregorian calendar, btw. All of this changes if not!)
Here is a little snippet of code that might help
var start = new DateTime(2009, 10, 12);
var end = new DateTime(2014, 12, 25);
var s = from j in Enumerable.Range(start.Year, end.Year + 1 - start.Year)
let _start = start.Year == j ? start : new DateTime(j, 1, 1)
let _end = end.Year == j ? end : new DateTime(j, 12, 31)
select new {
year = j,
days = Convert.ToInt32((_end - _start).TotalDays) + 1
};
If I understood your problem correctly, solving it using Inclusion Exclusion principle would be the best.
Say, if your start date is somewhere in 2008 and the end date is in 2010:
NDAYS(start, end) * coeff_2008
- NDAYS(2009, end) * coeff_2008 + NDAYS(2009, end) * coeff_2009
- NDAYS(2010, end) * coeff_2009 + NDAYS(2010, end) * coeff_2010
Where Ndays computes the number of dates in the interval (TotalDays plus one day).
There is no need to handle leap years specially or compute december 31st.
The details you can work out in a for-loop going over jan first of each year in the span.

Calculate days in years and months?

How to calculate days in years and months in c#?
per example :
If
1. days = 385 then I need to display Year= 1.1 (i.e. 1 year 1 month)
2. days= 234 then I need to display year =0.7 (i.e 0 year 7 months)
How can we calculate in c#?
I did for days=234 /365 and result is coming 0.64 (i.e. 0 year 6 months). But actually it is 7 months.
How to get accurate year and months.
You can do:
double temp = (234.0 /365.0) * 12.0;
int years = (int)temp;
int months = (int)(temp - years);
This is because you were getting 0.64, which is 0.64 years. If you want months, you'd need to multiply that times 12.
In the above, you'll get 0 years and 7 months... That being said, I'm not sure exactly how you want to format this:
string yearsString = string.Format("{0}.{1}", years, months);
Just be aware that this will do 3.11 for 11 months, which is going to be odd, though it was your requirement.
Also, if you want to have this be very general, you might want to use 365.25 instead of 365 to represent a single Julian Year, as it will help you reduce issues due to leap years.
If you don't know the actual dates, then you could estimate:
Number of years: x / 365
Number of months: (x % 365) / 30
where % is modulo
Assuming a month of exactly one-twelfth of a year, and that you want ignore partial months (based on your saying you expect 7 from your example with 7.688 months, then:
int days = 234;
double years = (double)days / 365.242199;
int wholeYears = (int)Math.Floor(years);
double partYears = years - wholeYears;
double approxMonths = partYears * 12;
string horribleFormat = string.Concat(wholeYears, ".", approxMonths);
Are you sure you want this format? The result--at least with the current information provided--will be fuzzy since months are inconsistent lengths.
Consider a couple alternatives:
A Year-only representation, such as 1.25 meaning "1 and one quarter years". This doesn't mix months and years, and as such remains simple since a year is 365 days (except for leap years). It also removes ambiguity such as "does 1.10 == 1.1?"
Using a concrete start date which would allow you to use strongly-type dates. You could easily use a .ToString() with date-formatting arguments to quickly and accurately get your result.
Let do some calculations.
1 mon = 0.1
2 mon = 0.2
.
.
9 mon = 0.9
10 mon = 1.0 [WRONG according to you 1 is a year]
fine then 1 / 12 = 0.083, therefore 0.083 is 1 month
Now,
234 / 365 = 0.64 => 0.64 / 0.083 => 7.7 i.e. 7th month
Therefore fx => days / 365 = ans % 0.083 = result.
I have no time to prove other number but you can try around this formula.
My suggestion would be to use DateTime.AddDays: it will give you all you need. You can also add other time units there:
DateTime f = new DateTime(0);
var y = f.AddDays(361);
Pseudo code
ts= TimeSpan.fromDays(385)
Years = ts.days Modulo 365
months = (ts.days remainder 365) modulo 12
days = (ts.days remainder 365) Remainder 12
answer string years + "." + months + "." + days
In VB
Dim d As DateTime = DateTime.MinValue 'year = 0001
d = d.AddYears(DateTime.Now.Year - 1) 'add current year
d = d.AddDays(234) 'add days
Dim yrs As Integer = d.Year - DateTime.Now.Year 'calculate years
Dim mos As Double = d.Month / 12 'calculate months
Dim answer As Double = yrs + mos
answer = .6666666666
I think that's numerically impossible. What would you do about 1 year and 11 months? 1.11? Because that could mean either 1 year and 1 month and something, or 1 year and 11 months.

Categories