将.Net日期与存储在SQL中的日期进行比较 [英] Comparing .Net Date with Date Stored in SQL

查看:78
本文介绍了将.Net日期与存储在SQL中的日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在传递C#中的日期,并将其与数据库中的日期字段进行比较以检索记录,但由于日期格式不匹配,所以无法这样做.
来自C#的日期就像''2011/11/24 10:59:50 AM''一样,而数据库中的日期是''2011-11-24 10:59: 50.360''
如何将C#日期转换为数据库格式?

I am passing a date from C# and comparing the same with a date field from the database to retrieve a record but not able to do so since the date format is not matching.
The date coming from C# is like ''11/24/2011 10:59:50 AM'' while the date in the database is ''2011-11-24 10:59:50.360''
How do I convert the C# date to the format of the Database?

推荐答案

DateTime a = GetFirstDate();
DateTime b = GetSecondDate(); // date from SQL

if (a.Date.Equals(b.Date))
{
    // the dates are equal
}


将日期从c#转换为以下格式:

字符串日期= DateTime.Now.ToString("mm-dd-yyyy hh:MM:ss.fff");

然后比较.

问候.
Covert date from c# to this format:

string date = DateTime.Now.ToString("mm-dd-yyyy hh:MM:ss.fff");

Then compare.

Regards.


您可以如下更改C#日期的格式

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")

这将为您提供与数据库日期格式相似的输出.
You can change the format of the C# Date as below

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")

This gives you the similar output as DataBase Date format is.


这篇关于将.Net日期与存储在SQL中的日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆