timespan

remove seconds from timespan using c#

随声附和 提交于 2019-12-09 10:20:54
问题 I want to remove the seconds from timespan using c# My code is here: TimeSpan lateaftertime = new TimeSpan(); lateaftertime = lateafter - Convert.ToDateTime(intime) ; It returns the value 00:10:00 But i want the below output : 00:10 only not seconds field :00 . 回答1: Well you can simply do as string.Format("{0}:{1}", ts.Hours,ts.Minutes) // it would display 2:5 EDIT to get it properly formatted use string.Format("{0:00}:{1:00}", ts.Hours,ts.Minutes) // it should display 02:05 回答2: Note that a

how to perform division in timespan [duplicate]

徘徊边缘 提交于 2019-12-09 07:46:40
问题 This question already has answers here : What is the best way to divide two TimeSpan objects? (4 answers) Closed 2 years ago . I have a value in TimeSpan , let's say: tsp1 = 2 hour 5 minutes. I have another TimeSpan variable which contains a value like: tsp2 = 0 hours 2 minutes Please tell me how I can divide tsp1 by tsp2 so that I can get the exact number of times tsp2 divides into tsp1 and what the remainder is. I am using Visual Studio 2008. Thanks. 回答1: The simplest approach is probably

Convert eg. 2012-05-25 to seconds since January 1 1970

青春壹個敷衍的年華 提交于 2019-12-09 05:57:18
问题 I have a database where I save time as time() from php, which is seconds since 1 jan 1970 . Is there any way I can convert, for example 2012-12-12 to seconds since 1 jan 1970 ? I want to do like: SELECT * FROM Table WHERE date > '2012-11-30' AND date < '2012-12-30' Is this even possible? (I would like to do it without any php date()) 回答1: DATEDIFF is going to be your friend here: select datediff(s, '1970-01-01', '2012-12-12') as SecondsSinceEpoch Note that because datediff returns an int, the

TimeSpan to friendly string library (C#)

与世无争的帅哥 提交于 2019-12-09 02:44:36
问题 Does anyone know of a good library (or code snippet) for converting a TimeSpan object to a "friendly" string such as: Two years, three months and four days One week and two days (It's for a document expiry system, where the expiry could be anything from a few days to several decades) Just to clarify, say I had a TimeSpan with 7 days, that should print "1 week", 14 days "2 weeks", 366 days "1 year and 1 day", etc etc. 回答1: Not a fully featured implementation, but it should get you close enough

Formatting TimeSpans

本小妞迷上赌 提交于 2019-12-08 15:01:46
问题 I am trying to format a TimeSpan with the following line of code: .ToString("[d.]hh:mm:ss") It throws a FormatException , but the exception goes away when I remove the : , the [] , and the . . I also cannot include spaces. Does anyone know why this is happening? On this msdn page it clearly states that you can include these characters. I am using .Net framework 4.5.2 btw. Thanks. 回答1: TimeSpan ts = new TimeSpan(5, 10, 44); string test = string.Format("{0:dd\\:hh\\:mm\\:ss\\.ffff}", ts); 回答2:

Crystal Report Function for converting Seconds to Timespan format

帅比萌擦擦* 提交于 2019-12-08 14:07:03
问题 I have a crystal report where it shows the Agent's activities throughout the day with a pie chart. In the details section it is displaying: Activity [string] StartedAt [DateTime] EndedAt [DateTime] Duration [The difference between EndedAt and StartedAt in seconds - Integer] Report data is GroupedBy Activity and summarized by Duration. Currently Duration is shown in seconds but I need to format it 02h:30m:22s:15ms. For that I wrote a custom function in Crystal Report in the Formula Workshop

Query for maximum number of concurrent time spans

隐身守侯 提交于 2019-12-07 03:10:20
问题 I have a SQL Server table with two datetime fields ( CnxStartdatetime , CnxEnddatetime ). Each row represents a transmission of information. I am trying to find the maximum number of concurrent transmissions based on these two timestamps . I have a working query but it is both slow and extremely cumbersome. I know there must be a better way to go about this but can't come up with any. For the current version, if I run it with 5 "levels" and get results I have to go back and add a ton of SQL

how to achieve timespan to string conversion?

那年仲夏 提交于 2019-12-06 22:52:46
问题 I tried searching here, but it couldn't help me much .. I want to convert time_span to string, I don't want to return the timespan in days .. but only HH:mm:ss. How to achieve that? My sample code is here: String time_span_par = "06:12:40"; String time_str = "18:13:59"; TimeSpan time_span_var = TimeSpan.Parse(time_span_par); TimeSpan time_span = TimeSpan.Parse(time_str); time_span = time_span.Add(time_span_var); string temp = time_span.ToString("HH:mm:ss"); 回答1: Try using DateTime d = new

Timespan-Regex with Days-Portion

只谈情不闲聊 提交于 2019-12-06 15:38:19
I've just tried several hundreds (if not thousands...) of RegEx's available to get what I want... But none of them worked. I'm simply looking for a Regular expression, that represents a TimeSpan days.hours:minutes:seconds : 7.00:00:00 would represent "7 Days" This one sadly doesn't work: (\d\d).(\d\d):(([0-6][0])|([0-5][0-9])):(([0-6][0])|([0-5][1-9])) That's because your Regex pattern is expecting 2 digits for days and you only have 1 digit. Just make the first digit optional with ? (\d?\d)\.(\d\d):(60|([0-5][0-9])):(60|([0-5][0-9])) or better yet just use + to match one or more because that

c# check if a timespan range is between timespan range and how many hours

半腔热情 提交于 2019-12-06 06:08:45
问题 Assuming I have 3 time ranges: 07:00 - 18:00 18:00 - 23:00 23:00 - 07:00 and the code: public class TimeShift { public TimeSpan Start { get; set; } public TimeSpan End { get; set; } } List<TimeShift> shifts = new List<TimeShift>(); How can I check if every item in the list is between the 3 ranges above and how many hours? For example one TimeShift where: Start: 07:00 End: 23:30 then that means 16.5 hours. For the examples above: Range 1: 11 hours Range 2: 5 hours Range 3: 0.5 hours 回答1: Here