Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Time Database Columns in .NET

RSS
Modified on Thu, Dec 02, 2010, 12:39 PM by Administrator Categorized as SQL Server, ┬ĚNet Framework

Overview

With SQL Server 2008 came (among other things), a new data type: time. By default a time field gets imported into Visual Studio as a TimeSpan .NET type. However, the TimeSpan type doesn't format in the familiar way that a DateTime does. This article describes how to format a TimeSpan using the same format string as for a DateTime.

Code

TimeSpan to String

TimeSpan ts = new TimeSpan(14, 43, 00);
DateTime dt = DateTime.Today.Add(ts);
string s = dt.ToString("h:mm tt");

String to TimeSpan

static TimeSpan ParseTimeSpan(string s)
{
    DateTime datum = DateTime.Today;
    TimeSpan result = DateTime.Parse(datum.ToString("MM/dd/yyyy") + " " + s).Subtract(datum);
    return result;
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.