I have been told to avoid using Environment.TickCount() as it overflows in about a month of uptime and can mess up my code, so I was wondering if DateTime.Now.Ticks() would be a safer choice for long terms ? I mean when will it overflow ?
46226
Is it ever OK to use Environment.TickCountto 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("That took " + duration.TotalMilliseconds + " ms");
5995
When displaying the value of a decimal currently with .ToString(), it's accurate to like 15 decimal places, and since I'm using it to represent dollars and cents, I only want the output to be 2 decimal places.   Do I use a variation of .ToString() for this?
77364
I have a program that continually polls the database for change in value of some field. It runs in the background and currently uses a while(true) and a sleep() method to set the interval. I am wondering if this is a good practice? And, what could be a more efficient way to implement this? The program is meant to run at all times.  Consequently, the only way to stop the program is by issuing a kill on the process ID. The program could be in the middle of a JDBC call. How could I go about terminating it more gracefully? I understand that the best option would be to devise some kind of exit strategy by using a flag that will be periodically checked by the thread. But, I am unable to think of a way/condition of changing the value of this flag. Any ideas?
63556
I found the question about converting epoch time to normal time, but I'm having two problems:   When I run DateTime.newInstance(1391529600 * 1000) I am getting the wrong answer I want to convert normal time to epoch time   1: I have been typing the following into Execute Anonymous:  system.debug(DateTime.newInstance(1391529600 * 1000));   By my calculations (and some websites I found) that should give me 4 Feb, 2014, 16:00:00 GMT.  Apex gives me      1969-12-31 12:56:36   Clearly, this is incorrect (and it's even before 1/1/1970). Can anyone replicate my situation?  2: I also want to change the current datetime (GMT) to epoch time, returning the number of seconds or milliseconds since 1/1/1970. I could break things into parts, but that seems an awful lot of script statements to do something that should be fairly easy, I'd hope.  Thanks for the help!
1637
The Today group has a nice useful total.  However, once 0:00 UTC rolls around, I can no longer see my score for yesterday. Please add a total for the Yesterday and Last 7 days groups.  On a broader note, how can you have a button with an icon of two graphs open a popup that never has any graphs?
45640
I have some issues about choosing how to align a Date/time data type in a field. Which is the best way to align this type of date and why? Right or left? P.S We use WPF.  [------25.03.2015] or [25.03.2015------]  Thank you :)
77297
Following up from my other , I would like to start thinking about what I should take a look at on daily/weekly/monthly bases in terms of alerts. I am hoping to be able to see problems coming before they happen (that is the plan)...  So far, I have made a start collecting scripts for the following (no order):  Daily   Check system uptime (just in case I need to check anything as a DBA) Check the last backup  Check the transaction log backups Check the status of SQL Jobs Check the average CPU usage for the last 24 hours (or 1140 mins)   Weekly   Check MSDB backup history Check to see when the last time CheckDB was run Check index fragmentation Check index stats (reads vs writes etc) Check for IO bottlenecks   Monthly   Check missing indexes Check indexes that are no longer used   Any other suggestions? (I am new to DBA so any help/advice is always welcome)
110293
I am trying to work out what script is required to have multiple timestamps on several tabs however each one I have tried doesn't work for me.  I am new to this. When text is added to column 2 I need a timestamp in column 1.  When text added in 8 I need a timestamp in 7.  When 15 which is a checkbox, a timestamp in 16, and when 18, timestamp in 17.  Is this possible to have so many? My current script is as follows and this is giving me the time stamp in column 1 when column 2 is amended and it doesnt overwrite when it is changed a second time. function onEdit(e) {   var row = e.range.getRow();   var col = e.range.getColumn();   if (col === 2 &amp;&amp; row &gt; 1 &amp;&amp; e.source.getActiveSheet().getName() === &quot;June 2021&quot;) {     if (e.source.getActiveSheet().getRange(row, 1).getValue() == &quot;&quot;) {       e.source.getActiveSheet().getRange(row, 1).setValue(new Date());     }   } }
14516
Is there an easy way within C# to check to see if a DateTime instance has been assigned a value or not?
51934
A few months ago I was introduced to the new  type and was glad 's flaws with regard to time zones were finally taken care of.  However, I was left wondering if there were any overhead or problems that could occur from using this new type.  I work on a multi-locale web application. Does anyone know of anything that could sway me from just using it for all my date/time work? Is there a window for abuse here?  Reference:
83143
When I am looking to create some timestamp fields (or other date/time style fields), what is the best way to name them? Should I just put record_timestamp?
4146
I need to get execution time in milliseconds.  I originally asked this question back in 2008. The accepted answer then was to use  However, we can all agree now  that using the standard  API is more appropriate. I am therefore changing the accepted answer to this one.
122301
When developing a general purpose time-series software, is it a good idea to make it scale invariant? How would one do that?  I took a time series of around 40 points, and then multiplied by factors ranging from 10E-9 to 10E3 and then ran through the ARIMA functions of Forecast Pro and Minitab. In Forecast Pro, all resulted in the same answer (automatic modeling), whereas in Minitab, they were not. Not sure what Forecast Pro does, but they might just scale up or down all the numbers to a certain scale (let's say 100s) before running the model. Is this good idea in general?
106192
In my application, users select a date range for which results are required. The UI looks something like this:    My users don't like this, because the date picker requires them to select a specific date. This is far more precise than they require - most of the results compiled by the system have results just monthly or quarterly. Some are only yearly.  The two ideas I've come up with are these:    On the left, just use a combobox to select from valid dates. The problem with this approach is that sometime the results will be daily and the valid date range goes back to the 1860's. That adds up to far too many items to handle in a combobox.  On the right, using a trio of comoboxes to select the date. This has some advantages. If the results are going to be monthly, I could disable the day combo; if the results are going to be quarterly, I could also restrict the items available in the month combo. But, it seems that this design requires a whole heap of clicks to use.  What's an alternative design for the UX that allows users to select just "useful" dates more easily?  My technology stack is C#, WinForms, .NET 3.5  Update Oct 1   The date ranges selected by my users are typically long - several years to several decades - pulling back sparse results over a long period.  The only common defaults that have been identified are "As early as possible" for the start date, and "Until today" as the finish date.    Lots of good ideas in the answers to date - thanks to all.  Update Oct 2   Some of the answers below (and I assume many of the readers) are assuming I need the date range for report generation. Actually, this particular part of my application is more of a query engine. If this detail inspires any new ideas, please answer away.
61
I need to find a bottleneck and need to accurately as possible measure time.  Is the following code snippet the best way to measure the performance?  DateTime startTime = DateTime.Now;  // Some execution process  DateTime endTime = DateTime.Now; TimeSpan totalTimeTaken = endTime.Subtract(startTime);
72625
Just this - How do you add a timer to a C# console application? It would be great if you could supply some example coding.
91496
Something's been bugging me about SO for a while now, that is the inaccuracy of "length of time" strings.  The best example I can give at the moment is .  If you look at the length of time he's been registered, it says 1 year.  He joined the site September 2008, which is only 3 months off two years.  It's the same with answers - you can give an answer 50 seconds before anyone else, but at certain times page views will show both as "5 minutes ago" or whatever.  That's not quite as bad, but with answers being sorted "randomly by votes" by default you could lose out on votes or being marked as the correct answer to the slower answer even if they're both the same.  There's probably a better method amongst these, IMO:   "Vulgar fraction" unicode characters     Member for 1½ years   answered 5¼ minutes ago asked 3⅓ hours ago  Different units   Member for 21 months (at least it's better than "1 year") Answered 306 seconds ago (ugly, I know) Asked 64 minutes ago (also ugly)  Combining units   Member for 1 year 9 months (fits easily on the profile screen) Answered 5 minutes 6 seconds ago (probably too long) Asked 3 hours 20 minutes ago (also long)  Rounding up   I personally prefer option 1, unicode fraction characters.  "Member for 21 months" is pretty good too because it's easy to quickly work out.  "Asked 452 seconds" ago is horrible though.  EDIT: I'm completely aware of the timestamp hover because I am forced into using it so much. It's a great feature, but it's not really in your face so not everyone knows about it. I still think there's a better "glancing" solution than having to hover over the time string.