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"));
}
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'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;
I have setup the timePicker as 24 hour mode, however if the time is currently 16:05 and I choose 16:10 from the timePicker, it says 12 hours and 5 minutes instead of 5 minutes. How can I easily swap the AM and PM?
In addition if I choose the same hour with minutes being smaller than current minutes, I will have an output such as 12 hours and -4 minutes .
Code:
public void onClickAlarmOn(View v) {
final Calendar c = Calendar.getInstance();
TimePicker alarm_time_picker = (TimePicker) findViewById(R.id.timePicker); //initializing timePicker before using it //http://stackoverflow.com/questions/25485768/why-does-my-app-keep-crashing-when-i-try-to-insert-a-date-and-time-picker
int hour = alarm_time_picker.getCurrentHour(); //get selected hour
int minute = alarm_time_picker.getCurrentMinute(); //get selected minute
int hour_now = c.get(Calendar.HOUR); //get system's hour
int minute_now = c.get(Calendar.MINUTE); //get system's minute
int hour_result = hour - hour_now; //subtract the time selected by time.now of system
int minute_result = minute - minute_now;
String hour_result_string = String.valueOf(hour_result); //convert to string to display
String minute_result_string = String.valueOf(minute_result);
setToast_result("Alarm set to " + hour_result_string + " hours " + minute_result_string + " minutes");
}
Thanks!
If you're using the 24-hour clock, you want to get Calendar.HOUR_OF_DAY from your Calendar instance, instead of Calendar.HOUR, which is based on the 12-hour clock.
int hour_now = c.get(Calendar.HOUR_OF_DAY);
I got a simple Julian Date Calculator with the following code:
DateTime date = DateTime.UtcNow;
int month = date.Month > 2 ? date.Month : date.Month + 12;
int year = month > 2 ? date.Year : date.Year - 1;
int hour = date.Hour;
int minute = date.Minute;
int second = date.Second;
int millisecond = date.Millisecond;
double day = date.Day + hour / 24.0 + minute / 1440.0 + (second + millisecond * 1000) / 86400.0;
int isJulianCalendar = isJulianDate(year, month, date.Day) ? 0 : 2 - year + year / 100 / 4;
When I run the program, it returns a lower value than the previous one (e.g if I run now, it shows a value, but if I run in a couple of minutes, it shows another value).
From the .pdf I copied the expression, it says that the formula use UT time. Is there any relevant difference from the UTC time?
.NET has a built in JulianCalendar class, which you should probably use instead of writing your own code.
double day = date.Day + hour / 24.0 + minute / 1440.0 + (second + millisecond * 1000) / 86400.0;
The (second + millisecond * 1000) part seems intended to calculate fractional seconds, but to get that, you need to divide millisecond by 1000.0, not multiply it.
Note that as I pointed out in the comments, this only addresses the immediate problem you are asking about, it will likely not be sufficient to actually correctly calculate the Julian day. However, since you yourself have posted links to working answers showing a calculation of the Julian day without time, you should be able to get it working from here on.