I'm trying to create an if statement that checks if there has been less than 48 hours since the creation of an order, i.e. COrderDate ,and there is still more than 48 hours until the delivery date. i.e. CDeliveryDate
if (order.COrderDate < 48 hours since DateTime.Now
&& DateTime.Now < 48 hours from order.CDeliveryDate)
You can subtract one DateTime from another and get a [TimeSpan] result which represents an amount of time. For example:
var timeSinceOrderDate = order.COrderDate - DateTime.Now;
You can then check that TimeSpan to see how many hours, minutes, days, etc. it contains.
if(timeSinceOrderDate.TotalHours >= 48)
This will tell you much time is left until the end of the universe:
var timeUntilEndOfTheUniverse = DateTime.MaxValue - DateTime.Now;
Related
I have a given time and date and I want to create a counter in my game that is counting down to this given date. I want to display the counter in this format: Days/Hours/Minutes/Seconds
How can I convert the DateTime {2/3/2020 12:00:00 AM} to something like this: 0 Days / 9 Hours / 30 Minutes / 20 Seconds ?
The counter should run until it has reached the given time {2/3/2020 12:00:00 AM} (0 Days / 0 Hours / 0 Minutes / 0 Seconds).
I get the following date and time from the server but I don't know how to make a counter out of it.
How can I create a counter that counts down to a given time and date?
var NextLeaderboardReset = resultleaderboard.Result.NextReset;
A DateTime minus a DateTime will give you a TimeSpan object, which is what you want here. Then you just need to get the correct string format for your countdown:
var countDownEnd = new DateTime(2020,2,3);
var timeSpan = DateTime.Now - countDownEnd;
var countDownString = $"Time left: {timeSpan.ToString(#"dd\:h\:m\:s")}";
Hi How Can I get a random date with hours and minutes in a range bewtween now and 2 or 3 days ago. ??
Thanks to all
something like this dates time with minutes
10/23/2018 4:32:00 PM
10/23/2018 5:31:00 PM
10/23/2018 1:32:00 AM
10/22/2018 2:00:00 PM
Here I can get the dates in a range, let say 2 days, but the hour is the same
public static DateTime NextDateTime(int endDatenumbers)
{
DateTime startDate = DateTime.Today;
DateTime endDate = startDate.AddDays(-endDatenumbers);
var newDate = startDate.AddHours(new Random(Convert.ToInt32(DateTime.Now.Ticks / int.MaxValue)).Next(0, (int)(endDate - startDate).TotalHours));
return newDate;
}
You should simplify that to 1 random call.
Get the furthest day which is 3 days ago.
var furthestDate= DateTime.Today.AddDays(-3);
You range is actually 2 days after that date which is (48hrs * 60 min) = 2880 minutes.
So anything from that date and 2880 minutes after is valid.
Simply get 1 random number between 0 an 2880. Finally simply add the minutes to the furthest date.
var randomDate = furthestDate.AddMinutes(YouRandomNumber);
The following logic actually computes the number of minutes between the two days. This is important where your days can potentially cross a daylight savings boundary. Also, I am storing the value "today" as technically (albeit unlikely) it could change between the two calls.
private static DateTime PickRandomMinute(int inPastNDays, Random random)
{
DateTime today = DateTime.Today;
int totalMinutes = (int)(today - today.AddDays(-inPastNDays)).TotalMinutes;
return today.AddDays(-inPastNDays).AddMinutes(random.Next(totalMinutes));
}
Example usage:
Random random = new Random();
Console.WriteLine(PickRandomMinute(2, random)); // 22/10/2018 9:34:00 PM (for example)
Console.WriteLine(PickRandomMinute(2, random)); // 23/10/2018 4:55:00 AM (for example)
You don't want to create a new Random within this method because calls that happen very close together would probably end up with the same seed and therefore return the same time.
I am try to find a nice way to work out the week number from a non standard starting date. Week 1 shall contain the first Sunday in April. To calculate this, I just loop through the first 7 days in April til I find the first Sunday. Weeks will start on Sunday.
Normally I would attempt to solve this doing something like this:
numberOfDaysDifferenceBetweenEpoch / 7 % 52 + 1;
However about every 5 years it works out as there are 53 weeks in a year. Obviously the function above will not work if it happens to be a 53 week year. An easy solution would be just to make two functions, which take the modulus of 52 or 53 however I'm hoping there is a cleaner way of doing this. What would the best way to approach this problem?
Here is one way that should work. You may want to optimise the GetEpochInYear method if you are using it frequently.
private static DateTime GetEpochInYear(int year)
{
DateTime currentYearEpoch = new DateTime(year, 4, 1);
while (currentYearEpoch.DayOfWeek != DayOfWeek.Sunday)
{
currentYearEpoch = currentYearEpoch.AddDays(1);
}
return currentYearEpoch;
}
private static int GetWeekNumber(DateTime dateOfInterest)
{
DateTime currentYearEpoch = GetEpochInYear(dateOfInterest.Year);
if (dateOfInterest < currentYearEpoch)
{
currentYearEpoch = GetEpochInYear(dateOfInterest.Year - 1);
}
int days = (int)(dateOfInterest - currentYearEpoch).TotalDays;
return (days / 7) +1;
}
I am having trouble comparing times.
From what I have researched it most likely is due to the time not having a date.
My code,
This gets a dateTime value from the database.
var getDateTime = sql.Staff_Time_TBLs.Where(p => p.Staff_No ==
SelectedEmployee.Key && p.Date_Data == day).Select(p => p.Time_Data_1).ToList();
DateTime dateTimeGet = Convert.ToDateTime(getDateTime);
dateTimeGet returns a value like this "2012/12/12 15:03:00.000"
I then declare variables to hold the time.
TimeSpan startCompare = TimeSpan.Parse("15:00");
TimeSpan endCompare = TimeSpan.Parse("21:00");
Then comparing the values Compare DateTime
if ((endCompare > dateTimeGet) && (startCompare < dateTimeGet))
{
//match found
}
I am getting a compile error,
operands cannot be given to to type timespan and datetime
How do I compare times in this situation?
Just edit your code like this:
if ((endCompare > dateTimeGet.TimeOfDay) && (startCompare < dateTimeGet.TimeOfDay))
{
//match found
}
You could create DateTime values instead of TimeSpan to compare the value, using the Date of your db time:
DateTime startCompare = dateTimeGet.Date.AddHours(15);
DateTime endCompare = dateTimeGet.Date.AddHours(21);
if ((endCompare > dateTimeGet) && (startCompare < dateTimeGet))
{
// match found
}
In the example you showed, actually would be enough to compare the Hour part of dateTimeGet:
if (dateTimeGet.Hour >= 15 && dateTimeGet.Hour <= 21)
// match found
Actually you are comparing time with date in endCompare > dateTimeGet so you are getting the error
operands cannot be given to to type timespan and datetime
To compare time-span you need to extract the time from date in dateTimeGet by simply using TimeOfDay.
if ((endCompare > dateTimeGet.TimeOfDay) && (startCompare < dateTimeGet.TimeOfDay))
{
//match found
}
This will convert the date into time. For more details about TimeOfDayclick here Hope this works fine for you.
The issue is that, as you rightly say, you are comparing dates to times
A time-span is a measurement of time measured in Hours, where as a date-time is a measurement of time measured in days
so 2012/12/12 15:03:00.000 is approximately 735248.625 days or 17645967 hours
which you are then comparing to a timespan of 15 hours
so you need to either add 735248 days to your time span or drop 735248 days form your Date
both can be easily done
If you call the time TimeOfDay property on the date it will ignore the days and just return 0.625 days as 15 hours
Which means your code would look like this
if ((endCompare > dateTimeGet.TimeOfDay ) && (startCompare < dateTimeGet.TimeOfDay))
OR
If you add the time span to the at midnight date it will create the correct date time for comparation
Which means your code would look like this
if ((dateTimeGet.Date + endCompare > dateTimeGet ) && (dateTimeGet.Date + startCompare < dateTimeGet.TimeOfDay))
I'm trying to figure an hour interval from a start time...
So if I allow the user to choose a value between 1 and 12 I want to figure out what times that represent in a 24 hour clock.
Lets say it is 9:00AM and they want to be notified every 4 hours during that day. I would need it to have the following values:
9AM
1PM
5PM
9PM
I'm trying to use the % (modulus) but I'm not getting what I'm expecting(4 % 24)... Any ideas?
Create a DateTime representing the current time (9:00 AM)
Create a TimeSpan representing the time interval (4 hours)
Use DateTime.Add(TimeSpan) method to produce the time of the next notification
The 12/24 hour clock does not play into it at all: you can format the next time the way you or your user wish - as a 24-hour clock using the "HH:mm" mask or as a 12-hour clock using "hh:mm" mask.
Here is a short code sample to get you started:
int hour = 9;
for (; hour <= 24; hour += 4)
Console.WriteLine("Hour = " + hour % 12);
Note the use of hour % 12.
Improvising... Use the % operator. As the % gets a value less than actual time (9:00AM) you know you need to change AM in PM... every time the result is less than actual time you have to change AM/PM or vice-versa.
int interval = 4;
int current = 9;
for(int i = current; i <= 24; i+=interval)
{
Console.WriteLine(i%12 + (i > 12 ? "PM" : "AM"));
}