Date compare like strtotime in C#? [duplicate] - c#

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;

Related

C# - UTC automatically convert to 12h system [duplicate]

This question already has answers here:
DateTime.Value.ToString(format) gives me 12 hour clock
(4 answers)
Closed 6 years ago.
I'm working with a project and one of its functional requirement is to create a datetime range in which I will select a set of data based on that.
This range should take today's datetime starting from 8:00:00 to 18:00:00, and then I want to convert the format to be yyyy-MM-ddThh:mm:ssZ, so I'm doing the following to approach that:
DateTime fromTime = DateTime.Now;
TimeSpan ts = new TimeSpan(8,0,0);
fromTime = fromTime.Date + ts;
string fromTimeFormat = fromTime.ToString("yyyy-MM-ddThh:mm:ssZ");
DateTime toTime = DateTime.Now;
TimeSpan tss = new TimeSpan(18, 0, 0);
toTime = toTime.Date + tss;
string toTimeFormat = toTime.ToString("yyyy-MM-ddThh:mm:ssZ");
The problem is that, the toTimeFormat is being converted to the 12h system, so when I use it later it's being considered as 6:00 AM.
Any ideas please?
Because you are using hh specifier which is for 12-hour clock format.
You need to use HH specifier which is for 24-hour clock format.
string toTimeFormat = toTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
And you can simplify your code as;
string fromTimeFormat = DateTime.Today.AddHours(6).ToString("yyyy-MM-ddThh:mm:ssZ");
string toTimeFormat = DateTime.Today.AddHours(18).ToString("yyyy-MM-ddTHH:mm:ssZ");

check if a date falls in the next 24 hours with c # [duplicate]

This question already has answers here:
How to know if a DateTime is between a DateRange in C#
(7 answers)
Closed 7 years ago.
it is possible with C # test whether the DateTime " 01/29/2016 16:22:00 " is included in the next 24 hours? I have a date that is " 01/28/2016 15:30 " ( DateTime.Now ) and I 'd like to know if it is within the range going forward 24 hours. I hope I explained .
(yourDatetime - DateTime.Now) <= TimeSpan.FromHours(24)
You can use AddHours:
var myDateTime = ...
var now = DateTime.Now;
if (myDateTime <= now.AddHours(24)
&& myDateTime >= now.AddHours(-24))
{
}
Also keep in mind that DateTime is immutable and they cannot be modified after creation. That is why AddHours returns a new instance.
Update:
After seeing M.kazem's answer I have though that you can also use that:
Math.Abs(myDateTime.Subtract(DateTime.Now).TotalHours) <= 24

calculate time from strings in dropdown lists [duplicate]

This question already has answers here:
Getting time span between two times in C#?
(6 answers)
Closed 7 years ago.
I have 2 dropdown lists holding times in 24 hour format, going up in increments of 5 minutes eg 00:00, 00:05, 00:10. Both lists are displaying strings
When a user selects a start and end time using these, I want to calculate the time difference but I'm not sure how to convert the format I have in the lists to workable times, can anyone help?
Im using C# in Visual Studio 2012.
The code below will show you an example of how to do this:
DateTime d1 = DateTime.Parse("00:00");
DateTime d2 = DateTime.Parse("00:05");
TimeSpan s1 = d2-d1;
Console.WriteLine(s1.TotalMinutes + " minutes difference");
You can replace the strings "00:00", and "00:05" with the values from the dropdown lists, and calculate the timespan between them.
You can use DateTime.ParseExact:
DateTime dt1 = DateTime.ParseExact(ddl1.SelectedValue, "HH:mm",DateTimeFormatInfo.InvariantInfo);
DateTime dt2 = DateTime.ParseExact(ddl2.SelectedValue, "HH:mm",DateTimeFormatInfo.InvariantInfo);
TimeSpan diff = dt2 - dt1;
Now you have all you need in the TimeSpan, f.e.:
int hours = diff.Hours; // 0 - 23
int minutes = diff.Minutes; // 0 - 59
int totalMinutes = (int) diff.TotalMinutes;

Get datetime value from X days go? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c#: whats the easiest way to subtract time?
I want
MyNewDateValue = MyDateNow - MyDateInteger;
Example
Today is the 22nd of December 2012
If MyDateIneger value is 120, MyNewDateValue, will return the datetime 120 days ago.
MyNewDateValue = MyDateNow.AddDays(-MyDateInteger);
Please look into DateTime.AddDays method
DateTime oneTwentyDaysAgo = DateTime.Today.AddDays(-120);
or in general
DateTime nDaysAgo = DateTime.Today.AddDays(-N);
// where N is the number of days
MyNewDateValue = MyDateNow.AddDays(-120);
or
MyNewDateValue = MyDateNow.AddDays(myVar);
Try this frnd
DateTime dt = new DateTime();
dt = DateTime.Now;
DateTime newdt = new DateTime();
TimeSpan tim = new TimeSpan(120,0,0,0,0);
newdt = dt.Add(tim);
MessageBox.Show(newdt.ToString());
ADD.timespan will help you to add or subtract days from today.

Date Range in days....C#

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.

Categories