I have a query that is calling an Oracle DB from C#. I want to write the query to get data that is, at most, 5 years old.
I currently have a hard coded value for public const int FIVE_YEARS_IN_DAYS = 1825;
But, this isn't correct because of leap years. Is there a function that will give me the correct number of days in the preceeding 5 years?
I think you want this:
DateTime now = DateTime.Now;
now.AddYears(-5).Subtract( now ).Days
DateTime now = DateTime.Now;
TimeSpan fiveYears = now.Subtract(now.AddYears(-5));
int numberOfDaysInLastFiveYears = fiveYears.Days;
This will correctly account for leap years. Doing this right now yields 1,826 days.
Related
I wanted to find if two DateTime are within a week.
One of them is the current system Datetime, which I got it by using:
DateTime CurrentDateTime = new DateTime();
CurrentDateTime = DateTime.Now;
The other DateTime will just be a selected date. Assuming it is stored in a variable called : ExportDate.
So, I can find the difference between them by doing
ExportDate.Subtract(CurrentDateTime)
But I cannot change this value into a int for comparing...
So how should I compare this to DateTime to see if this two dates are greater then 0Days, and less then 7Days.
Subtracting two datetimes gives you a TimeSpan. This class comes with a property called TotalDays.
Gets the value of the current TimeSpan structure expressed in whole and fractional days.
Src: https://msdn.microsoft.com/en-Us/library/system.timespan(v=vs.110).aspx
You can use to count the number of days I guess.
Regards,
Seb
Try to compare the TotalDays of the resulting TimeSpan. Depending on which is later, the result must be between -7 and 7. Use Math.Abs to avoid comparing the value against both bounds:
bool isWithinWeek = Math.Abs(ExportDate - CurrentDate).TotalDays) < 7
If the time of day is irrelevant, compare only the dates:
bool isWithinWeek = Math.Abs(ExportDate.Date - CurrentDate.Date).TotalDays) < 7
You can use
(ExportDate - CurrentDateTime).TotalDays <= 7
You can simply use the DayOfYear property of DateTime to get the day in the year and then check if the difference is less than 7 or not. You can try:
bool isWithinAWeek = Math.Abs(currentDateTime.DayOfYear-exportDate.DayOfYear)>7?false:true;
This question already has answers here:
strtotime equivalent in .NET
(6 answers)
Closed 9 years ago.
Have big problem I don´t know how to compare date in C# like PHP.
I want to convert this code in to C#:
$now = strtotime ("now");
$then = strtotime ("$date");
$difference = $now - $then ;
$num = ($difference/86400)/7;
$weeks = intval($num);
Which function should I use?
This link can´t find a matching function like strtotime.
Please help me to convert the code to C#.
Thanks in advance.
var now = DateTime.Now.TimeOfDay;
DateTime dt = DateTime.Parse(yourDateTime);
var then = dt.ToString("HH:mm");
// here you can do rest of the calculations
basically
dt.ToString("HH:mm"); // 24 hour clock
dt.ToString("hh:mm tt"); // 12 hour clock
Hope it will give you better understanding
I think you might be looking for something like below:
DateTime sDateTimeNow = DateTime.Now;//Gets the date time from the server now
DateTime sIn30Seconds = DateTime.Now.AddSeconds(30);//Gets date time and adds 30 Seconds
DateTime sIn30Hours = DateTime.Now.AddHours(30);//Gets date time and adds 30 hours
DateTime sIn30Days = DateTime.Now.AddDays(30);//Gets date time and adds 30 days
double dTotalDays = (sIn30Days - sDateTimeNow).TotalDays;
double dTotalHours = (sIn30Hours - sDateTimeNow).TotalHours;
double dTotalSeconds = (sIn30Seconds - sDateTimeNow).TotalSeconds;
I am writing a program in c# and I need to do some math with dates. I need to let the user enter a date, calculate the date that is 91 days later, and then find the month right after that. ex: user enters date of 1/15/12, it should add 91 days, 4/15/12, and then return a date of 5/1/12. Unfortunately, I have no idea how to do this and I couldn't find anything that was helpful.
var oldDate = <your_datetime_variable>.AddDays(91);
var newDate = new DateTime(oldDate.Year, oldDate.Month, 1).AddMonths(1);
Since constructing a new DateTime object has been suggested, here is another approach:
DateTime when = <user_supplied_date>;
DateTime future = when.AddDays(91);
when = future.AddDays(-(future.Day - 1)).AddMonths(1);
This is a little cryptic, but results in a one-liner that you could use.
I'm trying to get the number of days (calculated byu datediff) in sql and the number of days in c# (calculated by DateTime.now.Substract) to be the same, but they return different results....
//returns 0
int reso = DateTime.Now.Subtract(expirationDate).Days;
vs
//returns 1
dateDiff(dd,getDate(),ExpirationDate)
In both cases, ExpirationDate is '10/1/2011 00:00:00', and the code and the DB are sitting on the same server. I want the return int to be the same. I suspect I'm missing something stupid... ideas??
dateDiff(dd,getDate(),ExpirationDate) Is doing a days comparison. DateTime.Now.Subtract(expirationDate).Days is doing a date and time
For example
SELECT dateDiff(dd,'10/1/2011 23:59:00' , '10/2/2011') returns one day even when only one minute apart.
If you want the same in C# you need to remove the time component
e.g.
DateTime dt1 = new DateTime(2011,10,1, 23,59,0);
DateTime dt2 = new DateTime(2011,10,2, 0,0,0);
Console.WriteLine((int) dt2.Subtract(dt1.Subtract(dt1.TimeOfDay)));
So in your case it would be something like
DateTime CurrentDate = DateTime.Now;
int reso = CurrentDate.Subtract(CurrentDate.TimeOfDay).Subtract(DateTime.expirationDate).Days;
I haven't tested it but I would not do
DateTime.Now.Subtract(DateTime.Now.Subtract.TimeOfDay)
Because the second call to Now wouldn't be guaranteeing to be the same as first call to Now
In any case Stealth Rabbi's answer seems more elegant anyway since you're looking for a TimeSpan not a DateTime
10/1/2011 is less than 1 day away from DateTime.Now. Since you're getting back a TimeSpan and then applying Days to it, you're getting back a TimeSpan that is < 1 day. So it'll return 0 Days.
Instead, just use the Date component of those DateTimes and it'll correctly report the number of days apart - like this:
DateTime now = DateTime.Now;
DateTime tomorrow = new DateTime(2011, 10, 1);
var val = (tomorrow.Date - now.Date).Days;
This will yield you 1 day.
I'm assuming you want the number of Total days, not the number of days from the largest previous unit. You'd want to use the TotalDays property. Also, you may find it easier to use the minus operator to do a subtraction
DateTime d1 = DateTime.Now;
DateTime d2 = new DateTime(2009, 1, 2);
TimeSpan difference = d1 - d2;
Console.WriteLine(difference.TotalDays); // Outputs (today):1001.46817997424
I have two dates as duedate as 03/03/2011 and returndate as 03/09/2011. I want to find the fine in double when I subtract duedate from returndate.How can duedate be incremented?
Following code might help you:
var dueDate = new DateTime(2011, 3, 3);
//if you want to increment six days
var dueDatePlus6Days = dueDate.AddDays(6);
//if you want to increment six months
var dueDatePlus6Months = dueDate.AddMonths(6);
var daysDiff1 = (dueDatePlus6Days - dueDate).TotalDays; //gives 6
var daysDiff2 = (dueDatePlus6Months - dueDate).TotalDays; //gives 184
The logical solution would seem to be the AddDays method, as in the other answers.
However, I try (in general) never to use floating point (i.e. a double) when working with monetary or date values.
DateTime contains a time component and AddDays takes a double as argument (fractional part becomes time), so I tend to avoid use of that method.
Instead, I use
dueDatePlusOne = dueDate.AddTicks(TimeSpan.TicksPerDay);
This should result in slightly faster execution too. Not that it still matters much on today's hardware, but I started out coding for microprocessors with a <1 MHz clock speed and old PDP-8's and -11's and stuff like that back in the 1970's, and some habits never die ;)
Assuming returnDate, dueDate are DateTime objects:
double extraDays = (returnDate - dueDate).TotalDays;
May this can help you
DateTime dt_duedate = DateTime.Now;
DateTime dt_returndate = DateTime.Now.AddDays(2);
System.TimeSpan diffResult = dt_returndate.Subtract(dt_duedate);