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);
Related
Here is the code: Need to modify this to run the service every 15th day and last day of every month
this.serviceTimer.Interval = 300000; // 5 mins
this.serviceTimer.Elapsed += new ElapsedEventHandler (this.serviceTimer_Click);
this.serviceTimer.Start();
Logger.WriteEventLog("Service Started");
Just small Business logic to be done as shown below :
Set time interval for every 24 hours
timer.Interval = 60000*60*24;
Then check for current month and get number of days in current month so you will get total number of days then divide total number of days by 2 so that you have 2 dates
1) last day of month ie total no of days in month
2) middle date of month ie divided by 2
check for current date with these 2 days if it is equal then go for timeElapsed event
{
this.serviceTimer.Elapsed += new ElapsedEventHandler (this.serviceTimer_Click);
}
else
{
do nothing
}
In your serviceTimer_Click
{
...
DateTime nextExecute = DateTime.Now.AddMOnth(1);
timer.Stop();
timer.Interval = (nextExecute - DatTime.Now).TotalMilliseconds;
timer.Start();
}
And to start it:
{
...
DateTime now = DateTime.Now;
DateTime firstExecute = new DateTime( now.Year, now.Month, 15 ); //add time if needed...
if ( firstExecute < now )
{
firstExecute.AddMonth( 1 );
}
timer.Interval = (firstExecute - now).TotalMilliseconds;
}
Not compiled, but you get the drift...
Edit
Speaking about drift: to avoid your execution drifting to a later point in time, you could use a more clever way to build the nextExecute DateTime.
I am working on a comments system and would love to show the time since the post was made instead of the actual time the post was made. Is there an easy way to do this?
Currently I pull the dateAdded
comment.DateAdded = DateTime.Now;
A few things:
Don't use DateTime.Now in a web application. The time zone of your server should be irrelevant. Since you're storing the time a post was made, you should use DateTime.UtcNow instead.
comment.DateAdded = DateTime.UtcNow;
Read: The Case Against DateTime.Now
You can then subtract the time the post was made from the current time.
TimeSpan elapsed = DateTime.UtcNow - comment.DateAdded;
Once you have a TimeSpan object, you can then use any of various methods and properties.
// to get the total hours elapsed
double hours = elapsed.TotalHours;
// to get the total minutes elapsed
double minutes = elapsed.TotalMinutes;
// to get a string output of the elapsed time in the default format
string s = elapsed.ToString();
Be careful not to mistake the Minutes and TotalMinutes properties. An elapsed time on 90 minutes will have TotalMinutes == 90.0, but Hours == 1 and Minutes == 30.
Use this HelperExtension
public static class TimeHelper{
public static string TimeSpanString(this DateTime date) {
var Now = DateTime.Now-date; //better to use DateTime.UtcNow
if(Now.Days>0){
return Now.Days+" Days "+Now.Hours+" Hours "+Now.Minutes+" Minutes";
}
if (Now.Hours > 0)
{
return Now.Hours + " Hours " + Now.Minutes + " Minutes";
}
return Now.Minutes + " Minutes";
}
}
Ex how to use it.
comment.DateAdded.TimeSpanString()
I have done the following piece of code to add the Hours and thus calculate total number of hours.
string hour1="48.40";
string hour2 = "45.35";
TimeSpan ts = new TimeSpan(int.Parse(hour1.Split('.')[0]),int.Parse(hour1.Split('.')[1]),
0);
TimeSpan ts1 = new TimeSpan(int.Parse(hour2.Split('.')[0]),int.Parse(hour2.Split('.')[1]),
0);
Double total = (ts.TotalHours) + (ts1.TotalHours);
The problem here is when i add hour1 and hour2 the total comes as 64.25 which actually should have been 64.15
This is just one of the test case, if i put hour1= 40.00 and hour2= 40.10 than the value in the total comes as 80.166666666666657 which actually should have been 80.10
can anyone help me understand what am i doing wrong and what is the correct way to add HOUR and get total number of hours ?
Actually you're getting correct result - just mixing minutes and fractional parts of hours.
80 hrs 10 mins is 80 1/6 hours
64 hours 15 mins is 64 1/4 hours
It gets a little strange when you have timestamps put into strings. But if you need to do it like this, this code should work
string hour1="48.40";
string hour2 = "45.35";
//find total number of minutes for each hour above
int minutes1 = int.Parse(hour1.Split('.')[0])*60+int.Parse(hour1.Split('.')[1]);
int minutes2 = int.Parse(hour2.Split('.')[0])*60+int.Parse(hour2.Split('.')[1]);
//calculate back to hours and minutes and reassemble as a string
string result = (minutes1+minutes2)/60+"."+(minutes1+minutes2)%60;
And I hope you are expecting 94.15 and not 64.15 in your example above.
You may use next code to get result you would like to:
string hour1 = "48.40";
string hour2 = "45.35";
TimeSpan ts = new TimeSpan(int.Parse(hour1.Split('.')[0]), int.Parse(hour1.Split('.')[1]),
0);
TimeSpan ts1 = new TimeSpan(int.Parse(hour2.Split('.')[0]), int.Parse(hour2.Split('.')[1]),
0);
TimeSpan total = ts + ts1;
int hours = (int)total.TotalHours;
int minutes = total.Minutes;
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"));
}
I have a minute value and i want to have to 2 string values one with how many hours and the other with the minutes, e.g.:
Value - 121 minutes
string hours = 2
string minutes = 1
Value - 58 minutes
string hours = 0
string minutes = 58
How can I work this out in C#?
var span = System.TimeSpan.FromMinutes(121);
var hours = ((int)span.TotalHours).ToString();
var minutes = span.Minutes.ToString();
The ToString() is because you asked for string values ...
TotalHours are the complete hours in the TimeSpan, they can be more than 24 (whereas the "Hours" field has a maximum of 24)
Oh, and on second thought: Why use the TimeSpan and not calculate it yourself? Because TimeSpan is already there debugged & tested by Microsoft, it has a nice clean interface (looking at the code you easily see whats going on without having to follow a calculation mentally) and it easily extends to further solutions. (Have the input in seconds? Use TimeSpan.FromSeconds(). Want the days? Use span.TotalDays ...)
Update:
I just noticed mistake in my answer: TotalHours returns a fractional value of all the hours, so we have to truncate it to an integer before converting it to a string.
Use a Timespan struct and its Parse method.
int value = 121;
int hours = value / 60; // 2
int minutes = value % 60; // 1
string strHours = hours.ToString();
string strMinutes = minutes.ToString();
int value = 121;
int hours = value / 60;
int minutes = value % 60;
int value = 121;
TimeSpan timeSpan = TimeSpan.FromMinutes(value);
// gives you the rounded down value of 2
int hours = timeSpan.Hours;
// gives you the minutes left of the hour
int minutes = value - (hours * 60);