如何在SQL Server 2008中指定12小时的时间格式

时间:2023-01-14 15:19:40

I am using SQL Server 2008 and I have a database file (mydata.mdf) in which I have a column in one of the table and that column has the datatype (Time) .

我正在使用SQL Server 2008,我有一个数据库文件(mydata.mdf),其中我有一个表中的列,该列具有数据类型(时间)。

I added this database file to my WPF-C# project (using VS2010) , but I had a problem and its as follows :

我将此数据库文件添加到我的WPF-C#项目(使用VS2010),但我遇到了问题,其内容如下:

This (Time) column treats time in 24-Hours system , but I want to use (12-Hour) system in my application , so is there a way to define a 12-Hour system time in SQL server2008 .

这个(时间)列在24小时系统中处理时间,但我想在我的应用程序中使用(12小时)系统,所以有没有办法在SQL server2008中定义12小时系统时间。

and if there isn't , what do you think is the best way to handle that ???

如果没有,你认为最好的办法是什么?

Pleeeeeeeeeeease help me ASAP because I'm in a hurry and I can't figure it out ...

Pleeeeeeeeeeease尽快帮助我,因为我很匆忙,我无法弄明白......

3 个解决方案

#1


2  

The time in the database is not "formatted". It is represented in some internal format (which you can Google for but shouldn't care about) that allows it to represent each moment in the day, to the supported level of precision.

数据库中的时间不是“格式化”的。它以某种内部格式表示(您可以使用Google但不应该关注它),它允许它表示当天的每个时刻,以及支持的精度级别。

The values are only formatted when your application converts them to strings for the purpose of displaying them to the user, and you have full control over this.

只有当您的应用程序将它们转换为字符串以便将其显示给用户时,才会格式化这些值,并且您可以完全控制它。

So if you have read a time into an instance of the CLR DateTime class, you can display as a 12-hour time (omitting the date) with value.ToString("h:mm:ss tt"). Custom formatting options are listed here.

因此,如果您已经读取了CLR DateTime类的实例的时间,则可以使用value.ToString(“h:mm:ss tt”)显示12小时的时间(省略日期)。此处列出了自定义格式选项。

#2


1  

The answer is you can't really, but don't worry, it's not a problem. Format the date in your C# code.

答案是你不能真的,但不要担心,这不是问题。在C#代码中格式化日期。

The point is that a date and time is an absolute value, which is what you want SQL to store, then 12 hour vs 24 hour clock is merely a display detail, eg, 13:00 and 1:00pm are equivalent, don't worry about how SQL stores it, then in C# use the following to display it:

关键是日期和时间是一个绝对值,这是你想要SQL存储的,那么12小时对24小时时钟只是一个显示细节,例如,13:00和下午1:00是等价的,不要担心SQL如何存储它,然后在C#中使用以下内容来显示它:

DateTime myDateTime = GetTheTimeFromSomeMethod();
myDateTime.ToString("h:mm:ss tt");

There are lots of guides, This is a good one, but there are plenty of others eg this one

有很多指南,这是一个很好的指南,但还有很多其他指南,例如这一个

#3


0  

Does it have to be formatted from the database? C# and WPF both provide many built-in date format options. For example, check out the ContentStringFormat property on a Label.

是否必须从数据库格式化? C#和WPF都提供了许多内置的日期格式选项。例如,查看Label上的ContentStringFormat属性。

If you must do it in the database, here is a messy workaround which will work

如果你必须在数据库中这样做,这是一个混乱的解决方案,将起作用

It formats the date as a string using a 12h clock, then removes the date part of it

它使用12h时钟将日期格式化为字符串,然后删除它的日期部分

select right(convert(varchar, cast('1/1/2010 23:59:59' as datetime), 100), 
charindex(' ', reverse(convert(varchar, cast('1/1/2010 23:59:59' as datetime), 100)))-1)

#1


2  

The time in the database is not "formatted". It is represented in some internal format (which you can Google for but shouldn't care about) that allows it to represent each moment in the day, to the supported level of precision.

数据库中的时间不是“格式化”的。它以某种内部格式表示(您可以使用Google但不应该关注它),它允许它表示当天的每个时刻,以及支持的精度级别。

The values are only formatted when your application converts them to strings for the purpose of displaying them to the user, and you have full control over this.

只有当您的应用程序将它们转换为字符串以便将其显示给用户时,才会格式化这些值,并且您可以完全控制它。

So if you have read a time into an instance of the CLR DateTime class, you can display as a 12-hour time (omitting the date) with value.ToString("h:mm:ss tt"). Custom formatting options are listed here.

因此,如果您已经读取了CLR DateTime类的实例的时间,则可以使用value.ToString(“h:mm:ss tt”)显示12小时的时间(省略日期)。此处列出了自定义格式选项。

#2


1  

The answer is you can't really, but don't worry, it's not a problem. Format the date in your C# code.

答案是你不能真的,但不要担心,这不是问题。在C#代码中格式化日期。

The point is that a date and time is an absolute value, which is what you want SQL to store, then 12 hour vs 24 hour clock is merely a display detail, eg, 13:00 and 1:00pm are equivalent, don't worry about how SQL stores it, then in C# use the following to display it:

关键是日期和时间是一个绝对值,这是你想要SQL存储的,那么12小时对24小时时钟只是一个显示细节,例如,13:00和下午1:00是等价的,不要担心SQL如何存储它,然后在C#中使用以下内容来显示它:

DateTime myDateTime = GetTheTimeFromSomeMethod();
myDateTime.ToString("h:mm:ss tt");

There are lots of guides, This is a good one, but there are plenty of others eg this one

有很多指南,这是一个很好的指南,但还有很多其他指南,例如这一个

#3


0  

Does it have to be formatted from the database? C# and WPF both provide many built-in date format options. For example, check out the ContentStringFormat property on a Label.

是否必须从数据库格式化? C#和WPF都提供了许多内置的日期格式选项。例如,查看Label上的ContentStringFormat属性。

If you must do it in the database, here is a messy workaround which will work

如果你必须在数据库中这样做,这是一个混乱的解决方案,将起作用

It formats the date as a string using a 12h clock, then removes the date part of it

它使用12h时钟将日期格式化为字符串,然后删除它的日期部分

select right(convert(varchar, cast('1/1/2010 23:59:59' as datetime), 100), 
charindex(' ', reverse(convert(varchar, cast('1/1/2010 23:59:59' as datetime), 100)))-1)