timespan

Random DateTime between range - not unified output

心不动则不痛 提交于 2019-11-26 17:49:19
问题 I implemented the below RandomDate, but I always keep getting values closed to "From" date, i probably miss something here.... public static DateTime GetRandomDate(DateTime from, DateTime to) { var range = new TimeSpan(to.Ticks - from.Ticks); var rnd = new Random(); var randTimeSpan = new TimeSpan((long)(range.TotalSeconds - rnd.Next(0, (int)range.TotalSeconds))); return from + randTimeSpan; } 回答1: You could change to: static readonly Random rnd = new Random(); public static DateTime

C#: what is the easiest way to subtract time?

耗尽温柔 提交于 2019-11-26 17:44:27
I'm trying to put together a tool that will help me make work schedules. What is the easiest way to solve the following? 8:00am + 5 hours = 1:00pm and 5:00pm - 2 hours = 3:00pm and 5:30pm - :45 = 4:45 and so on. Steve Townsend These can all be done with DateTime.Add(TimeSpan) since it supports positive and negative timespans. DateTime original = new DateTime(year, month, day, 8, 0, 0); DateTime updated = original.Add(new TimeSpan(5,0,0)); DateTime original = new DateTime(year, month, day, 17, 0, 0); DateTime updated = original.Add(new TimeSpan(-2,0,0)); DateTime original = new DateTime(year,

Merge pandas dataframes where one value is between two others [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 17:29:33
This question already has an answer here: How to join two dataframes for which column values are within a certain range? 5 answers I need to merge two pandas dataframes on an identifier and a condition where a date in one dataframe is between two dates in the other dataframe. Dataframe A has a date ("fdate") and an ID ("cusip"): I need to merge this with this dataframe B: on A.cusip==B.ncusip and A.fdate is between B.namedt and B.nameenddt . In SQL this would be trivial, but the only way I can see how to do this in pandas is to first merge unconditionally on the identifier, and then filter on

Timespan formatting [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-26 17:00:19
问题 This question already has answers here : How can I String.Format a TimeSpan object with a custom format in .NET? (19 answers) Closed 2 years ago . How do you elegantly format a timespan to say example "1 hour 10 minutes" when you have declared it as : TimeSpan t = new TimeSpan(0, 70, 0); ? I am of course aware that you could do some simple maths for this, but I was kinda hoping that there is something in .NET to handle this for me - for more complicated scenarios Duplicate of How can I String

Measuring code execution time

你离开我真会死。 提交于 2019-11-26 14:21:48
I want to know how much time a procedure/function/order takes to finish, for testing purposes. This is what I did but my method is wrong 'cause if the difference of seconds is 0 can't return the elapsed milliseconds: Notice the sleep value is 500 ms so elapsed seconds is 0 then it can't return milliseconds. Dim Execution_Start As System.DateTime = System.DateTime.Now Threading.Thread.Sleep(500) Dim Execution_End As System.DateTime = System.DateTime.Now MsgBox(String.Format("H:{0} M:{1} S:{2} MS:{3}", _ DateDiff(DateInterval.Hour, Execution_Start, Execution_End), _ DateDiff(DateInterval.Minute,

Find if current time falls in a time range

心已入冬 提交于 2019-11-26 14:14:03
Using .NET 3.5 I want to determine if the current time falls in a time range. So far I have the currentime: DateTime currentTime = new DateTime(); currentTime.TimeOfDay; I'm blanking out on how to get the time range converted and compared. Would this work? if (Convert.ToDateTime("11:59") <= currentTime.TimeOfDay && Convert.ToDateTime("13:01") >= currentTime.TimeOfDay) { //match found } UPDATE1: Thanks everyone for your suggestions. I wasn't familiar with the TimeSpan function. For checking for a time of day use: TimeSpan start = new TimeSpan(10, 0, 0); //10 o'clock TimeSpan end = new TimeSpan

How do I convert a TimeSpan to a formatted string? [duplicate]

亡梦爱人 提交于 2019-11-26 14:14:03
This question already has an answer here: How can I String.Format a TimeSpan object with a custom format in .NET? 19 answers Timespan formatting [duplicate] 5 answers I have two DateTime vars, beginTime and endTime. I have gotten the difference of them by doing the following: TimeSpan dateDifference = endTime.Subtract(beginTime); How can I now return a string of this in hh hrs, mm mins, ss secs format using C#. If the difference was 00:06:32.4458750 It should return this 00 hrs, 06 mins, 32 secs Would TimeSpan.ToString() do the trick for you? If not, it looks like the code sample on that page

ActionScript 3.0 + Calculate timespan between two dates?

旧城冷巷雨未停 提交于 2019-11-26 13:56:12
问题 In ActionScript 3.0, is there an automatic way to calculate the number of days, hours, minutes and seconds between two specified dates? Basicly, what I need is the ActionScript equivalent of the .NET Timespan class. Any idea? 回答1: I created an ActionScript TimeSpan class with a similar API to System.TimeSpan to fill that void, but there are differences due to the lack of operator overloading. You can use it like so: TimeSpan.fromDates(later, earlier).totalDays; Below is the code for the class

Format TimeSpan greater than 24 hour

感情迁移 提交于 2019-11-26 13:51:27
Say I convert some seconds into the TimeSpan object like this: Dim sec = 1254234568 Dim t As TimeSpan = TimeSpan.FromSeconds(sec) How do I format the TimeSpan object into a format like the following: >105hr 56mn 47sec Is there a built-in function or do I need to write a custom function? Jon Skeet Well, the simplest thing to do is to format this yourself, e.g. return string.Format("{0}hr {1}mn {2}sec", (int) span.TotalHours, span.Minutes, span.Seconds); In VB: Public Shared Function FormatTimeSpan(span As TimeSpan) As String Return String.Format("{0}hr {1}mn {2}sec", _ CInt(Math.Truncate(span

Environment.TickCount vs DateTime.Now

非 Y 不嫁゛ 提交于 2019-11-26 12:56:08
Is it ever OK to use Environment.TickCount to calculate time spans? int start = Environment.TickCount; // Do stuff int duration = Environment.TickCount - start; Console.WriteLine("That took " + duration " ms"); Because TickCount is signed and will rollover after 25 days (it takes 50 days to hit all 32 bits, but you have to scrap the signed bit if you want to make any sense of the math), it seems like it's too risky to be useful. I'm using DateTime.Now instead. Is this the best way to do this? DateTime start = DateTime.Now; // Do stuff TimeSpan duration = DateTime.Now - start; Console.WriteLine