C#windows客户端服务器应用程序中的DateTime问题 [英] DateTime issues in C# windows client server application
问题描述
Hello Experts
迟来的圣诞快乐和新年快乐
我在C#客户端服务器应用程序中遇到关键日期问题
在我的客户端服务器应用程序中,我在一个系统和客户端应用程序中安装了我的服务器(数据库)
在这些具有不同日期时间格式的系统中,我很难将字符串转换为日期时间
在服务器系统中,日期时间格式为dd / MM / yyyy hh:mm:ss tt
客户1中的
日期时间格式为MM / dd / yyyy hh:mm:ss tt
客户2中的
日期时间格式为客户3中的yyyy / MM / dd hh:mm:ss tt
日期时间格式为dd-MM-yyyy hh:mm:ss tt
如何在这种棘手的情况下将字符串转换为日期时间,我无法让我的客户将每个系统中的日期时间格式更改为唯一的日期时间格式:(
我用Google搜索了一下关于这些,但似乎没有什么可以帮助我
请帮助我专家:(
让我知道你是否需要更清楚这篇文章。
Hello Experts
Belated Merry Christmas and advanced happy new year
I have an critical issue with Datetime in C# client server application
In my Client server application, i have my server (database) in one system and client applications installed in many other systems
I have tough time in converting string to datetime in these systems which have different date time formats
Say, in server system the datetime format is dd/MM/yyyy hh:mm:ss tt
in Client 1 the datetime format is MM/dd/yyyy hh:mm:ss tt
in Client 2 the datetime format is yyyy/MM/dd hh:mm:ss tt
in Client 3 the datetime format is dd-MM-yyyy hh:mm:ss tt
How do i convert string to datetime in this tricky situation, i cant ask my client to change to datetime format in each system to a unique datetime format :(
I have googled a lot on these but nothing seems to help me out
Please help me experts :(
Let me know if you need more clarity on this post.
推荐答案
这就是为什么将DateTimes作为字符串存储在数据库中是一种不好的做法,应该不惜一切代价避免。
最好的办法是:
- 将值存储为数据库中的DateTime,最好是UTC;
- 根据本地时间/区域格式在客户端上显示值。下面是一些有用的方法的有用链接。
DateTime.ToLocalTime方法 [ ^ ]
DateTime.ToUniversalTime Method [ ^ ]
DateTime.ToString Method(IFormatProvider) [ ^ ]
协调世界时 [ ^ ]
示例:
存储DateTime值在客户端的数据库中:
That's why storing DateTimes as string in database is a bad practice and should be avoided at all costs.
The best to do is :
- to store values as DateTime in the database, preferably in UTC ;
- to display values on clients according to local time/regional format. Below are some useful links about methods you can use for that.
DateTime.ToLocalTime Method[^]
DateTime.ToUniversalTime Method[^]
DateTime.ToString Method (IFormatProvider)[^]
Coordinated Universal Time[^]
Examples :
Storing a DateTime value in database from client:
DateTime date = dateTimePicker.Value.ToUniversalTime();
cmd.CommandText = "UPDATE Invoices SET InvoiceDate = @myDate WHERE InvoiceId = @currentId";
cmd.Parameters.AddWithValue("@myDate", date);
cmd.Parameters.AddWithValue("@currentId", currentId);
cmd.ExecuteNonQuery();
在客户端应用程序上检索并显示DateTime值:
Retrieving and displaying a DateTime value on client application:
cmd.CommandText = "SELECT InvoiceId, InvoiceDate FROM Invoices";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
DateTime currentDate = ((DateTime)dr["InvoiceDate"]).ToLocalTime();
invoiceDateTextBox.Text = currentDate.ToString(CultureInfo.CurrentUICulture);
}
通过这种方式,您不必再费心了解DateTime存储和显示格式。
这些只是一些骷髅,给出了关于DateTime处理的基本想法;我希望你能全面了解并能够使它适应你当前的代码。
This way you won't have to bother anymore about DateTime storage and display formats.
These are just some skeletons and give a basic idea about DateTime handling; I hope you get the big picture and are able to adapt it to your current code.
你好
请使用以下
Hello
Please use following
string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
string format = "dd/MM/yyyy hh:mm:ss tt";
DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
更新:
UPDATES:
You can set any format
string dateString1 = "Mon 16 Jun 8:30 AM 2008";
string dateString2 = "16 Jun 2008 8:30 AM";
string dateString3 = "2008 Jun 16 8:30 AM";
string format = "dd/MM/yyyy hh:mm:ss tt";
DateTime dateTime = DateTime.ParseExact(dateString1, format, CultureInfo.InvariantCulture);
DateTime dateTime = DateTime.ParseExact(dateString2, format, CultureInfo.InvariantCulture);
DateTime dateTime = DateTime.ParseExact(dateString3, format, CultureInfo.InvariantCulture);
输出将是所有三个日期
Output will be for all three date
16/Jun/2008 08:30:45 AM
谢谢,
Imdadhusen
Thanks,
Imdadhusen
这篇关于C#windows客户端服务器应用程序中的DateTime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!