Compare the Days of Two Dates [duplicate] - c#

This question already has answers here:
DateTime difference in days on the basis of Date only
(3 answers)
Closed 8 years ago.
I'm using ASP.NET, C# and SQL Server. In the database the date is: 2014-10-28. Just year, month and day, no time portion.
I have this code:
DateTime data1 = new DateTime();
DateTime data2 = DateTime.Now;
data1 = reader.GetDateTime(3);
double total= (data2 - data1).TotalDays;
Response.Write(total.ToString());
The only problem of this is the output. The output of this is "4,81351624131366". Because probably this is the difference of the hours. Its possible to set the data2 to give me just the Y,M and D?
Or convert the total into days?

The property TotalDays returns the fractional portion of days too. Since the date you're comparing was 4 days ago at midnight, you're getting 4 full days and an additional 4/5th of a day (since it's not exactly midnight when you're executing this).
Either what Jon said (use DateTime.Today instead of DateTime.Now to get the date portion with a time of midnight), or use the Days property, which will just return a rounded 4 and drops the fractional portion.

Related

RemainingDays not returning correct count [duplicate]

This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 10 months ago.
In this sample I am trying to get the RemainingDays from two dates. One of the day is the next day and the other is two days from now. I am using dateTime.now and getting
Code
int? RemainingDays = null;
DateTime? EndDateTest1 = new DateTime(2022, 5, 11);
RemainingDays = ( EndDateTest1.Value - DateTime.Now ).Days;
Current Output
0
1
Thinking it should be
1
2
Working sample
https://dotnetfiddle.net/bYcLeZ
A DateTime is not a date, but, as the name implies, a date and a time.
If you initialize a DateTime with only a date, the time will be 00:00:00.
The time of DateTime.Now() will actually have a value, so when you subtract that from your EndDateTest1, the result will actually be less than the full number of days you expect. If you run this around 8 P.M., the result of the subtraction is 4 hours, which is, indeed, 0 days.
You should be able to resolve by only getting date portion via
RemainingDays = ( EndDateTest1.Value - DateTime.Now.Date ).Days;
The .Date ending of a date/time field will truncate any time portion for you.

I need help figuring out this oracle db timestamp [duplicate]

This question already has answers here:
Convert epoch to date in sqlplus / Oracle
(2 answers)
Closed 3 years ago.
I am running a query on an oracle db and am trying to get the start time for this particular event. The date is just stored as a number in the table and the only way I have been able to retrieve it is with TO_CHAR(TIMEFRAME_START). This gives me an output of 1574402400.
I know that this should correspond to 11/21/2019 11:00:00 PM MST based on a different app that pulls the same information but I have no idea how it does this conversion. I need to retrieve additional rows and need to be able to convert that output to a standard date format.
Does anyone have any ideas on how I can accurately format the original output?
Assuming that the number represents a unix timestamp, you can turn it to a date with the following expression:
to_date('1970-01-01', 'yyyy-mm-dd') + timeframe_start / 60 /60 / 24
Rationale:
unix epoch starts on January 1st, 1970
in Oracle, you can add a fractional number of days to a date
So basically we would convert the epoch timestamp to a number of days by dividing it by 60 (seconds per minutes), 60 (minutes per hour), and 24 (hours per day), and then add it to the date that represents epoch start.
You can then format the resulting date to the desired string format with to_char():
to_char(
to_date('1970-01-01', 'yyyy-mm-dd') + timeframe_start / 60 /60 / 24,
'mm/dd/yyyy hh12:mi:ss am'
)
You should do the following since that is a UNIX timestamp:
TO_CHAR(TO_DATE('19700101000000','YYYYMMDDHH24MISS') + numtodsinterval(TO_CHAR(TIMEFRAME_START), 'SECOND'), 'MM/DD/YYYY HH:MI:SS AM')

How to get today 00:00 time [duplicate]

This question already has answers here:
How to get the current date without the time?
(14 answers)
Closed 3 years ago.
how can I get today datetime from 00:00, for example when I Use:
var dt=DateTime.Now; // 2019/1/1 15:22:22
I need Another extention method to give this string format:
string today = dt.TodayBegining(); // 2019/1/1 00:00:00
Just
DateTime.Today
Doc: DateTime.Today Property
An object that is set to today's date, with the time component set to 00:00:00.

C# Adding years and days in a single DateTime variable

Am I missing something simple?
I am trying to calculate a date 17 years and 364 days before the given date.
Is there a way to do this without converting everything into days? I am trying to avoid dealing with leap years. I am doing the following:
DateTime date = Convert.ToDateTime(tId2);
string tId4a = Convert.ToString(tId4);
var age1 = tId4a.Substring(0, 2);
int age2 = Convert.ToInt32(age1) - 1;
DateTime sub1 = date.AddYears(-age2);
I was hoping to do something simple like:
DateTime sub1 = date.AddYears(-age2) + date.AddDays(-364);
I am being told that I cannot use the '+' in the DateTime.
Sorry, but I am new to this. The reason the age2 variable is used is because at times that value will change. But, the 364 should be consistent. I am creating something to test a date boundary.
Did I overlook something simple?
Thanks.
What you do is you add the "date age2 years ago" to the "date 364 days ago".
Instead do this:
DateTime sub1 = date.AddYears(-age2).AddDays(-364)
This at first subtracts the years and then subtracts the days from the resulting value.
You can't add dates, but you can certainly chain method calls together
date.AddYears(-age2).AddDays(-364);
This is for all intents and purposes the same thing as trying to add them together.
It really sounds like you want to go with tid4 years ago, but go to the next day after that.
The way you are doing it, is that you subtract 1 from that to get age2. Then you subtract that many years, and you also subtract 364 days from your date. This will be more sensitive to leap years. If the resulting date happens to be between Jan 1 and Feb 28 of a leap year, you will end up with one day later than you wanted.
364 is a very suspect number. I tend to think you are using that to mean "the number of days in a year minus one". But the number of days in a year is not always 365. In leap years, the number of days is 366. In such years, subtracting 364 is not 1 day less than a year. It is actually 2 days less than a year, so you would be off.
What you really should do, if I am reading you correct, is to just subtract the number of years, then add one day back in.
DateTime date = Convert.ToDateTime(tId2);
string tId4a = Convert.ToString(tId4);
int age = Convert.ToInt32(tId4a.Substring(0, 2))
DateTime sub1 = date.AddYears(-age).AddDays(1);
I think that it is valuable to mention that DateTime is an object, and that .AddYears(), .AddDays(), etc all return a new DateTime object which is why you cannot add them together like primitive types. So when you run:
DateTime sub1 = date.AddYears(-age2).AddDays(-364);
date.AddYears(-age2) returns a new object, and then .AddDays(-364) is using the new DateTime object and not the date instance.
For more info:
https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

DateTime.Today vs DateTime.Now [duplicate]

This question already has answers here:
Difference between System.DateTime.Now and System.DateTime.Today
(8 answers)
Closed 10 years ago.
These give me different dates
DateTime.Now.ToUniversalTime().ToString(#"yyyy-MM-dd");
DateTime.Today.ToUniversalTime().ToString(#"yyyy-MM-dd");
Why? I'm assuming it has something to do with the "time portion" of the datetime, perhaps set to 0-0-0.
The DateTime.Today property actually returns DateTime.Now.Date: And it's time segment is looks like 00:00.00000. And the DateTime.Now time segment is looks like 10:09.00000. So when you are converting to the ToUniversalTime it will depends on the current time.
public static DateTime Today {
get {
DateTime now = DateTime.Now;
return now.Date;
}
}
Because of ToUniversalTime().
From MSDN
The Coordinated Universal Time (UTC) is equal to the local time minus the UTC offset. For more information about the UTC offset, see TimeZone.GetUtcOffset. The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.

Categories

Resources