datetime

How do you calculate accumulative time in C#?

I want to calculate the time span between 2 times which I saved in a database. So literally I want to know the length of time between the 2 values. 14:10:20 - 10:05:15 = 02:05:05 So the result would be 02:05:05. How would I be able to achieve this using C#? 14:10:20 is the format I saved it in in my database. ...

.NET - How to hide invalid choices in a DateTimePicker

I've set the MaxDate and MinDate properties of a DateTimePicker. However, when I test the control at runtime, there is no way to tell the invalid dates from the valid ones. The only difference is that clicking on an invalid date does nothing. This is not very intuitive for the user. I want to be able to tell at a glance what the valid d...

Get integer value of the current year in Java

I need to determine the current year in Java as an integer. I would like to be able to use this value as a counter that I can, for example, use to run from now until a specified year in the past (i.e. my value starts at 2008, and I have a "firstYearOfData" value that is set to 1994. I can now run my process from 2008 back to 1994). I cou...

What is the best way to parse a time into a Date object from user input in Javascript?

I am working on a form widget for users to enter a time of day into a text input (for a calendar application). Using JavaScript (we are using jQuery FWIW), I want to find the best way to parse the text that the user enters into a JavaScript Date() object so I can easily perform comparisons and other things on it. I tried the parse() met...

Calculating the elapsed working hours between 2 datetime.

Given two datetimes. What is the best way to calculate the number of working hours between them. Considering the working hours are Mon 8 - 5.30, and Tue-Fri 8.30 - 5.30, and that potentially any day could be a public holiday. This is my effort, seem hideously inefficient but in terms of the number of iterations and that the IsWorkingDay...

How would you store and query hours of operation?

We're building an app that stores "hours of operation" for various businesses. What is the easiest way to represent this data so you can easily check if an item is open? Some options: Segment out blocks (every 15 minutes) that you can mark "open/closed". Checking involves seeing if the "open" bit is set for the desired time (a bit lik...

Expressing Excel formula in Java (decimal to time interpretation)

Hi, I am converting an excel sheet formula to java but I can't understand how excel manages to take the following: 0.22 Applies a formula: =TEXT(R5/14, "h:mm") and somehow arrives at: 0.22 Again if I provide: 2.8 it arrives at 4.48 Can someone please explain to me how it does this. I have read a little regarding decimal and I understand...

Is there a better way to trim a DateTime to a specific precision?

What's the best way to trim a DateTime object to a specific precision? For instance, if I have a DateTime with a value of '2008-09-29 09:41:43' and but I only want it's precision to be to the minute, is there are a better way to do it than this? private static DateTime TrimDateToMinute(DateTime date) { return new DateTime( ...

How to iterate over a timespan after days, hours, weeks and months in Python?

How do I iterate over a timespan after days, hours, weeks or months? Something like: for date in foo(from_date, to_date, delta=HOURS): print date Where foo is a function, returning an iterator. I've been looking at the calendar module, but that only works for one specific year or month, not between dates. ...

Changing short date format in Ubuntu

How do I change the system-wide short date format in Ubuntu? For example, Thunderbird is showing dates in the DD/MM/YY format, and I would like to change it to MM/DD/YY or YYYY-MM-DD. The best information I can find so far is in this thread: http://ubuntuforums.org/showthread.php?t=193916 Edit: I want to change the system-wide date f...

What is wrong with DateTime.Parse(myString)?

I was browsing Scott Hanselman's Developer Interview question list, and ran across this question: What is wrong with DateTime.Parse(myString)? While I know there are inherent risks in parsing a string of unknow format or origin, are there other reasons? Is it to use DateTime.ParseExact instead? Should it be myString.ToString() fi...

Why do these two date formats differ?

I'm trying to produce just the day number in a WPF text block, without leading zeroes and without extra space padding (which throws off the layout). The first produces the day number with a space, the second produces the entire date. According to the docs, 'd' should produce the day (1-31). string.Format("{0:d }", DateTime.Today); strin...

How to render contextual difference between two timestamps in JavaScript?

Let's say I've got two strings in JavaScript: var date1 = '2008-10-03T20:24Z' var date2 = '2008-10-04T12:24Z' How would I come to a result like so: '4 weeks ago' or 'in about 15 minutes' (should support past and future). There are solutions out there for the past diffs, but I've yet to find one with support for future time diff...

PHP DateTime microseconds always returns 0

Hi, this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new DateTime(); echo $dt->format("Y-m-d\TH:i:s.u") . "\n"; ?> Output: [root@www1 ~]$ php date_test.php 2008-10-03T20:31:26.000000 [root@www1 ~]$ php date_test.php 2008-10-03T20:31:27.000000 [root@www1 ~]$ php date_test.php 2008-10-03T20:31:27.000000 [root@www1 ...

DateTime, DateTime? and LINQ

When I retrieve a record using LINQ that has a DateTime field only the ToString() is available. Where are all the other DateTime methods? I have to Convert.ToDateTime the DateTime? that the Field returns? What is the difference between (DateTime) and (DateTime?) ...

How do I elegantly print the date in RFC822 format in Perl?

How can I elegantly print the date in RFC822 format in Perl? ...

Converting mysql TIME from 24 HR to AM/PM format

Hi, I want to display the TIME field from my mysql table on my website, but rather than showing 21:00:00 etc I want to show 8:00 PM. I need a function/code to do this or even any pointers in the right direction. Will mark the first reply with some code as the correct reply. ...

How to get records after a certain time using SQL datetime field

If I have a datetime field, how do I get just records created later than a certain time, ignoring the date altogether? It's a logging table, it tells when people are connecting and doing something in our application. I want to find out how often people are on later than 5pm. (Sorry - it is SQL Server. But this could be useful for o...

Average difference between dates in Python

I have a series of datetime objects and would like to calculate the average delta between them. For example, if the input was (2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00), then the average delta would be exactly 00:10:00, or 10 minutes. Any suggestions on how to calculate this using Python? ...

C# - Convert UTC/GMT time to local time

We are developing a C# application for a web-service client. This will run on Windows XP PC's. One of the fields returned by the web service is a DateTime field. The server returns a field in GMT format i.e. with a "Z" at the end. However, we found that .NET seems to do some kind of implicit conversion and the time was always 12 hours ...