Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Input value is only like this in string value and nothing else: P0DT3H40M0S
This indicates travel time and needs to be converted to hours and minutes.
D signifies Day, T is Time (right part signifies Time, above sample was: 3 Hours And 40 Minutes and 0 Seconds. H-hours, M-Minutes, S-Seconds)
If there's a value in D, eg. P1DT3H40M0S - this already signifies 1 Day and 3 Hours and 40 minutes and 0 Seconds, so output should be 27 Hours and 40 minutes. How do i get the values for Days, Hours and minutes using Regex? or can i use substring? my problem is if there were 2 digit Days and Hours, substring won't work. i need to get the numbers leftside the D, H and M.
You can use TimeSpan.ParseExact to parse this string into a TimeSpan
var pattern=#"\Pd\D\Th\Hm\Ms\S";
var ts = TimeSpan.ParseExact("P01DT3H40M0S",pattern,CultureInfo.InvariantCulture);
You can use the TimeSpan object in calculations. It supports addition, subtraction and comparison.
You can extract the total duration in hours using TotalHours. In this case, this will return 27.6666666. You can get the whole hours just by casting to int, (int)ts.TotalHours will return 27.
Copying from one of Jon Skeet's answers you can format the string as hours/minutes by using the truncated TotalHours and the Minutes, Seconds component:
String.Format("{0}:{1}:{2}",(int)ts.TotalHours,ts.Minutes,ts.Seconds)
This will return 27:40:0
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 13 days ago.
Improve this question
There is a SQLite data table with a lastVisited column which is REAL type .
I hope to delete rows using C# where lastVisited column are older than 30 days (This is a parameter, maybe it's 45, 60, or other), how can I do?
REAL is one the supported data type for storing dates :
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November
24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Julian days would be in the 2400000..2500000 range, and you can delete old values with something like :
DELETE ...
WHERE lastVisited < julianday('now') - 30
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to compare the current hour to see if it's past 16 h (4 o clock in the afternoon)
int temps = new TimeSpan(16, 0, 0).Hours;
int now = DateTime.Now.TimeOfDay.Hours;
int result=1;
if (now > temps)
result = 2;
but if I want now to be displayed like this 14h or 15h without having to do this
now.ToString("MM/dd/yyyy HH:mm")
because I need to compare it to temps so I need it as an integer
is it possible? All my research led to advising me to use the to string format
I have tried : int now = DateTime.NowUTC.TimeOfDay.Hours; which displayed 17h but the problem is that it's the time in the UK and I live in Canada
A DateTime is always stored the same way internally, no matter how it is displayed. Whenever you see a date, it means that is has been converted to a string and formatted. Even in the debugger! What you see is just a temporary representation of the real DateTime stored as a number.
The documentation of the DateTime.Hour Property says:
The value of the Hour property is always expressed using a 24-hour clock.
This real DateTime is internally stored as the time which elapsed since a reference date. The documentation of the DateTime Structure says:
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds). For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight.
You can write
int now = DateTime.Now.Hour; // Always expressed using a 24-hour clock.
int result = 1;
if (now > 16)
result = 2;
Or more concise
int result = DateTime.Now.Hour > 16 ? 2 : 1;
Don't use NowUTC if you want your local time. This yields what was once called Greenwich Mean Time (GMT).
This question already has an answer here:
Dividing HH:MM:SS by an integer
(1 answer)
Closed 5 years ago.
Is there a easy solution for dividing a time? For example divide 04:00:30 by two and get 2:00:15?
select FIRMA, NR, CAST(Zeit AS TIME(0)) as Zeit
from [2_2017]
where Art='D'
and Zeit is not Null
and Zeit !='00:00:00'and Tour_1 ='0'
order by Firma,NR
Perhaps you first need to convert time to some integer as sec, min , hours from midnight and then devide it and add back to midnight
DECLARE #zeint time = '23:00:15'
declare #midnight time ='00:00:00'
select
dateadd(SECOND,
(DATEDIFF(SECOND,#midnight, #zeint)/2)
,#midnight) as half
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm a newbie in C# programming and I need your help on this one.I have a string with a value of "12:45:00" and I would like to convert it into decimal with two decimal places.
Though you can actually use DateTime struct for your case, but since your string does not contain date info, I think the easiest way would be to use string.Split instead.
string[] strs = "12:45:00".Split(':'); //will give you 12, 45, and 00
double val = Convert.ToDouble(strs[0]) + Convert.ToDouble(strs[1]) / 60 + Convert.ToDouble(strs[2]) / 3600;
Then to print it in two decimal values, simply do:
val.ToString("F2"); //you will get 12.75
Or, to get 12.45, then simply do:
double val = Convert.ToDouble(strs[0]) + Convert.ToDouble(strs[1]) / 100; //note 100 here - second doesn't matter here
If your intention is to convert a duration in hours, minutes and (optionally) seconds into hours, you can do that like so:
double answer = TimeSpan.Parse("12:45:00", CultureInfo.InvariantCulture).TotalHours;
Console.WriteLine(answer); // Prints 12.75
This is the value that you can use along with an hourly rate to calculate a gross income.
Note: It would be incorrect to convert 12:45 (hh:mm) into 12.45, because 12:45 is 12.75 hours, not 12.45 hours.
This question already has answers here:
Add hours or minutes to the current time
(4 answers)
Closed 7 years ago.
I get the current time and need to add 1 minute to it so It would be 1 minute ahead, how do I do that?
string date1 = System.DateTime.Now.ToString("HH:mm");
All you have to use is DateTime.AddMinutes
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
Conversely, if you want to subtract a minute just use a negative parameter.
DateTime.AddMinutes method can do that.
If you want a string representation then do:
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
As a side note, you can also supply a negative number to subtract minutes from current time like:
DateTime.Now.AddMinutes(-1)