How to perform multiplication in date (datetimeoffset format) - c#

I have following 3 fields
startingdate, expirydate, number of months
startingdate = DateTimeOffset.Now;
and number of months, say 24 months
How to calculate expirydate = ?
Can anybody give me an idea?

You don't need multiplication in this case - just addition, specifying the units:
DateTimeOffset startDate = DateTimeOffset.Now;
DateTimeOffset expiryDate = startDate.AddMonths(months);
Two things to note:
Date and time arithmetic can be odd. In your example case it's less likely to be odd than normal, as you've got 2 years, so the only corner case is adding 2 years to February 29th and getting February 28th; normally you'd need to consider (say) adding 1 month to August 31st and getting September 30th. In other words, just because two expiry dates are the same doesn't mean they came from the same start date.
You might want to consider using DateTimeOffset.UtcNow and doing everything in UTC, rather than using the local time zone. Using DateTimeOffset instead of DateTime protects you from time zone problems to some extent, but keeping everything in UTC is clearer.
If you really mean you have dates rather than dates and times, you might want to explicitly use midnight... it's unfortunate that .NET doesn't have any "date-only" type. You might want to consider using my Noda Time which is designed to make things rather clearer than the BCL API.

Related

Get Milliseconds from Ticks in DateTime c# [duplicate]

This question already has answers here:
How do you convert epoch time in C#?
(14 answers)
Closed 2 years ago.
I have a simple DateTime object, equal to the date: 11/1/2020 8:11:14 AM.
I want to convert it to milliseconds so I do:
myTimestamp?.Ticks / TimeSpan.TicksPerMillisecond.
I get 63739786274788, which seems correct from the pure calculation perspective.
However, when I input it into one of the online converters to validate, I get the date Wed Nov 01 3989 01:11:14, which is of course way off.
Questions:
What is this number 63739786274788 if not time in ms?
How do I get "normal" timestamp in ms?
In .NET, DateTime ticks are based on an epoch of 0001-01-01T00:00:00.0000000. The .Kind property is used to decide whether that is UTC, local time, or "unspecified".
Most online converters, such as the one you linked to, are expecting a Unix Timestamp, where the value is based on an epoch of 1970-01-01T00:00:00.000Z. It is always UTC based. (The precision varies, both seconds and milliseconds are commonly used.)
If you want to get a milliseconds-based Unix Timestamp From .NET, instead of dividing you should use the built-in functions DateTimeOffset.FromUnixTimeMilliseconds and DateTimeOffset.ToUnixTimeMilliseconds. (There are also seconds-based versions of these functions.)
Assuming your input values are UTC-based:
DateTime dt = new DateTime(2020, 11, 1, 8, 11, 14, DateTimeKind.Utc);
DateTimeOffset dto = new DateTimeOffset(dt);
long timestamp = dto.ToUnixTimeMilliseconds();
// output: 1604218274000
DateTimeKind.Local will also work with this, assuming your values are indeed based on the computer's local time zone. DateTimeKind.Unspecified is a bit trickier, as you'll need to convert to a DateTimeOffset with a specific time zone using TimeZoneInfo first.
You could also construct the DateTimeOffset value directly, rather than go through DateTime at all.
Okay, so you start off dividing Ticks by TicksPerMillisecond (10,000)
As you can see, the number you generated is much larger than the current milliseconds:
63739786274788
1607363529803
The short answer is that Ticks are based off of 12:00:00 midnight January 1, 0001 and a your online calculator is based off of unix time, January 1, 1970. So that would explain why you're about 2,000 years off. If you subtracted the Ticks from a new DateTime(1970,1,1), then that would give you about the right number to satisfy the online calculator.
For more info, I would suggest reading through MS's docs on DateTime

Converting datetime to time in C# / ASP.NET

I am trying to insert time on my asp.net project.
RequestUpdateEmployeeDTR requestUpdateEmployeeDTR = new RequestUpdateEmployeeDTR();
requestUpdateEmployeeDTR.AttendanceDeducID = int.Parse(txtAttendanceDeducID.Text);
requestUpdateEmployeeDTR.TimeInChange = txtTimeOutChange.Text;
requestUpdateEmployeeDTR.TimeOutChange = txtTimeOutChange.Text;
TimeInChange and TimeOutChange are DateTime data types. But I am inserting a time data type. How can I convert that into a time data type using C#? Thanks!
The .NET Framework does not have a native Time data type to represent a time of day. You will have to decide between one of the three following options:
Option 1
Use a DateTime type, and ignore the date portion. Pick a date that's outside of a normal range of values for your application. I typically use 0001-01-01, which is conveniently available as DateTime.MinValue.
If you are parsing a time from a string, the easiest way to do this is with the DateTimeStyles.NoCurrentDateDefault option. Without this option, it would use today's date instead of the min date.
DateTime myTime = DateTime.Parse("12:34", CultureInfo.InvariantCulture,
DateTimeStyles.NoCurrentDateDefault);
// Result: 0001-01-01 12:34:00
Of course, if you prefer to use today's date, you can do that. I just think it confuses the issue because you might be looking to apply this to some other date entirely.
Note that once you have a DateTime value, you can use the .TimeOfDay property to get at just the time portion, represented as a TimeSpan, which leads to option 2...
Option 2
Use a TimeSpan type, but be careful in how you interpret it. Understand that TimeSpan is first and foremost a type for representing an elapsed duration of time, not a time of day. That means it can store more than 24 hours, and it can also store negative values to represent moving backwards in time.
When you use it as a time of day, you might be inclined to think of it as "elapsed time since midnight". This, however, will get you into trouble because there are days where midnight does not exist in the local time zone.
For example, October 20th 2013 in Brazil started at 1:00 AM due to daylight saving time. So a TimeSpan of 8:00 on this day would actually have been only 7 hours elapsed since 1:00, not 8 hours elapsed since midnight.
Even in the United States, for locations that use daylight saving time, this value is misleading. For example, November 3rd 2013 in Los Angeles had a duplicated hour for when DST rolled back. So a TimeSpan of 8:00 on this day would actually had 9 hours elapsed since midnight.
So if you use this option, just be careful to treat it as the representative time value that matches a clock, and not as "time elapsed since midnight".
You can get it directly from a string with the following code:
TimeSpan myTime = TimeSpan.Parse("12:34", CultureInfo.InvariantCulture);
Option 3
Use a library that has a true "time of day" type. You'll find this in Noda Time, which offers a much better API for working with date and time in .NET.
The type that represents a "time of day" is called LocalTime, and you can get one from a string like this:
var pattern = LocalTimePattern.CreateWithInvariantCulture("HH:mm");
LocalTime myTime = pattern.Parse("12:34").Value;
Since it appears from your question that you are working with time and attendance data, I strongly suggest you use Noda Time for all your date and time needs. It will force you to put more thought into what you are doing. In the process, you will avoid the pitfalls that can come about with the built-in date/time types.
If you are storing a Time type in your database (such as SQL server), that gets translated as a TimeSpan in .Net. So if you go with this option, you'll need to convert the LocalTime to a TimeSpan as follows:
TimeSpan ts = new TimeSpan(myTime.TickOfDay);

Get dayspan according to dates, not duration?

I am trying to get the number of days between two datetimes, but not according to the exact timespan, rather according to the "day date" difference between date a and date b. No hours taken in account.
So far, I calculated the age of an item using :
(DateTime.Now - creationDate).Days
The problem with this code is that, for something that was created the day before but less than 24h ago, it will output "0 days", which is not very clear to my users apparently.
So what I want to accomplish is, even for an item that was created at 11:59pm for example, to get an output of "1 day old" as soon as the clock hits midnight. How could I accomplish this?
Two options:
1) Take the two dates first:
var days = (DateTime.Today - creationDate.Date).Days;
2) Use my Noda Time date/time library instead, using LocalDate for the two dates involved, and then Period.Between(start, end, PeriodUnits.Days) to get a period of just days. (You can also get weeks, months etc.) Admittedly getting "today's date" is deliberately a bit trickier in Noda Time - where .NET implicitly uses "the system time zone," Noda Time forces you to be more explicit.
I would use it simply like
int days =(int)DateTime.Now.Subtract(creationDate).TotalDays;
hope this helps

Compare date less than 3 months

I need to compare whether date is less than 3 months old.
I will get installation date:
DateTime installdate=DateTime.Parse("1/5/2012 8:12:14 PM");
if ((installdate<DateTime.Now.AddMonths(-3)))
{
// do something
}
Is this the best way to compare the dates?
Thanks.
A few things to think about:
"Is date x earlier than 3 months before today" isn't the same as "today is more than 3 months later than date x"; you'll need to make sure you have the exact semantics you want.
Consider what you want to do with the time component - are you interested in dates or dates and times? (Would you expect the condition evaluation to change based on the current time of day?)
Consider time zones: are you interested in "today in the system's current time zone" or some fixed time zone?
Depending on the source of the text data, you should possibly use DateTime.TryParse and you should possibly use DateTime.ParseExact or DateTime.TryParseExact, passing in the expected format (and culture)
Basically, there are various corner cases around date and time behaviour - you should explicitly think about all of these things (some of which are forced upon you if you use Noda Time instead of DateTime, btw :)
Regarding the first point, if the idea is that you get a trial period of three months from the installation date (or something similar), that suggests you should be adding three months to that instead.
I'd also change the variable name and get rid of the redundant parentheses, by the way:
DateTime installationDate = DateTime.Parse("1/5/2012 8:12:14 PM");
DateTime trialPeriodEnd = installationDate.AddMonths(3);
if (trialPeriodEnd > DateTime.Now)
{
// do something
}
Assuming you're storing the installation date yourself somewhere, I would try to store it in some form which is less ambiguous - possibly even storing just a "ticks" value instead of a string. But assuming you are storing it yourself, you shouldn't need to use TryParse - it makes sense to go "bang" if you can't parse the value. I'd use ParseExact, probably with a standard format specifier of "o" (round trip).
DateTime installdate ;
if (DateTime.TryParse("1/5/2012 8:12:14 PM", out installdate))
{
if ((installdate < DateTime.Now.AddMonths(-3))) { }
}
Tryparse is used so as to validate if the date passed in the parameter is valid or invalid

c# calc time diff between 2 countries incl DST

i want to know the time difference between two countries.
There is ofcourse the static time difference, but during some periods the daylight saving time comes in between.
As far as i know the dst period is also different for some countries, so june 1 the diff between country a and b can be 1 hour, 1 july it can be 2 hours due to DST, and 1 august it can be 1 again etc etc
Is there a framework function for it or do i have to calculate it myself?
Michel
You need to know:
Both time zones (use TimeZoneInfo from .NET 3.5, bearing in mind that one country can have several time zones)
An instant in time, e.g. a UTC DateTime or a DateTimeOffset.
At that point it's relatively easy: convert the UTC instant into the local time in both time zones using TimeZoneInfo.ConvertTime, and subtract one from the other. Alternatively, use TimeZoneInfo.GetUtcOffset for both of them, and subtract one offset from the other.
Here's an example to find the current difference between London and Mountain View:
using System;
class Test
{
static void Main()
{
var mountainView = TimeZoneInfo.FindSystemTimeZoneById
("Pacific Standard Time");
var london = TimeZoneInfo.FindSystemTimeZoneById
("GMT Standard Time");
DateTimeOffset now = DateTimeOffset.UtcNow;
TimeSpan mountainViewOffset = mountainView.GetUtcOffset(now);
TimeSpan londonOffset = london.GetUtcOffset(now);
Console.WriteLine(londonOffset-mountainViewOffset); // 8 hours
}
}
To find out any historical difference (ie. for a date in the past when local DST policies for one or both timezones were different) you will have to store past policies for DST start/end/offset for each timezone and work them out yourself.
If you're fine with knowing the difference based on the current set of rules loaded into Windows then the built-in .NET method is straightforward and easy.

Categories