timespan

How to convert a double value to a DateTime in c#?

二次信任 提交于 2019-12-18 15:25:09
问题 I have the value 40880.051388 and am storing it as a double, if I open Excel and paste in a cell and apply the following custom format " m/d/yyyy h:mm" to that cell, I get "12/3/2011 1:14" How can I do this parsing/Conversion in C#? I don't know if the value is milliseconds from a certain checkpoint, like epoch time, or if the value is in some specific prepared format, but how does excel come up with this particular value? Can it be done in C#? I've tried working with TimeSpan , DateTime ,

TimeSpan “pretty time” format in C#

匆匆过客 提交于 2019-12-18 14:13:09
问题 Typing in the title to this question brought me to this question. I'm looking for the same thing, but something perhaps less statically formatted if you get what I mean? I'm writing a quick program that will be taking a TimeSpan duration of two DateTime objects and outputting them for printing to paper. The format that would be preferred is: XX days, YY hours, ZZ minutes (seconds are irrelevant; as are days because I don't expect the timespan to incorporate more than a few hours). Say the

Subtracting two dates

て烟熏妆下的殇ゞ 提交于 2019-12-18 11:41:31
问题 I have two calendars and each return a DateTime from calendar.SelectedDate. How do I go about subtracting the two selected dates from each other, giving me the amount of days between the two selections? There is a calendar.Subtract() but it needs a TimeSpan instead of DateTime. 回答1: You can use someDateTime.Subtract(otherDateTime) , this returns a TimeSpan which has a TotalDays property. 回答2: Just use: TimeSpan difference = end - start; double days = difference.TotalDays; Note that if you

How do I convert ticks to minutes?

牧云@^-^@ 提交于 2019-12-18 10:44:10
问题 I have a ticks value of 28000000000 which should be 480 minutes but how can I be sure? How do I convert a ticks value to minutes? 回答1: TimeSpan.FromTicks(28000000000).TotalMinutes; 回答2: A single tick represents one hundred nanoseconds or one ten-millionth of a second. FROM MSDN. So 28 000 000 000 * 1/10 000 000 = 2 800 sec. 2 800 sec /60 = 46.6666min Or you can do it programmaticly with TimeSpan: static void Main() { TimeSpan ts = TimeSpan.FromTicks(28000000000); double minutesFromTs = ts

How do I convert a 12 hour time string into a C# TimeSpan?

喜你入骨 提交于 2019-12-18 03:58:16
问题 When a user fills out a form, they use a dropdown to denote what time they would like to schedule the test for. This drop down contains of all times of the day in 15 minute increments in the 12 hour AM/PM form. So for example, if the user selects 4:15 pm, the server sends the string "4:15 PM" to the webserver with the form submittion. I need to some how convert this string into a Timespan, so I can store it in my database's time field (with linq to sql). Anyone know of a good way to convert

Is TimeSpan unnecessary?

跟風遠走 提交于 2019-12-18 02:39:47
问题 EDIT 2009-Nov-04 OK, so it's been a little while since I first posted this question. It seems to me that many of the initial responders failed to really get what I was saying--a common response was some variation on "What you're saying doesn't make any sense"--and so I've made some handy diagrams to really illustrate my point. When we speak of numbers, we are generally referring to points on what grade school children learn is called the Number Line: Now, when we learn arithmetic, our minds

Creating countdown to date C#

安稳与你 提交于 2019-12-17 20:26:18
问题 I want to make a Windows Form Application which only shows a timer as: xx days xx hours xx minutes xx seconds No option for setting the timer or anything, i want to do that in the code However, the problem is i want it to count down from current time (DateTime.Now) to a specific date. So i end up with the time left as TimeSpan type. I'm now in doubt how to actually display this, so it's actually working, and updating (counting down) Can't seem to find a tutorial that helps me, so i hope i may

TimeSpan.ToString(“hh:mm”) error [duplicate]

牧云@^-^@ 提交于 2019-12-17 20:17:19
问题 This question already has answers here : Why does DateTime.Now.TimeOfDay.ToString(“HH:mm:ss.ffffff”) throw FormatException? (2 answers) Closed 4 years ago . Why I got an error when I want to get the string of a TimeSpan with a custom format. DateTime.Now.TimeOfDay.ToString("hh:mm"); // Error: Input string was not in a correct format. 回答1: DateTime.Now.TimeOfDay.ToString(@"hh\:mm\:ss") Documentation 回答2: According to MSDN TimeOfDay is a TimeSpan. And in the examples of TimeSpan.ToString you

Why isn't my TimeSpan.Add() working?

狂风中的少年 提交于 2019-12-17 19:07:52
问题 There has to be an easy answer: var totalTime = TimeSpan.Zero; foreach (var timesheet in timeSheets) { //assume "time" is a correct, positive TimeSpan var time = timesheet.EndTime - timesheet.StartTime; totalTime.Add(time); } There's only one value in the list timeSheets and it is a positive TimeSpan (verified on local inspection). 回答1: TimeSpans are value types. Try: totalTime = totalTime.Add(time) 回答2: This is a common mistake. TimeSpan.Add returns a new instance of TimeSpan . 回答3:

A Real Timespan Object With .Years & .Months

坚强是说给别人听的谎言 提交于 2019-12-17 15:36:25
问题 Consider the following 2 scenarios: Scenario 1). Today is May 1st 2012, and Scenario 2). Today is September 1st 2012. Now, consider that we write on our webpage the following about a comment someone has left: "This comment was written 3 months and 12 days ago". The amount of days in both these scenarios will ALWAYS be different even though the statement is exactly the same. In Scenario 1, "3 months and 12 days" would equal 102 days . However, in Scenario 2, "3 months and 12 days" would be 104