简体   繁体   English

将数据库中的日期与DateTime.Now比较

[英]Comparing Dates in Database to DateTime.Now

I have database table in which the dates are stored in the following format 我有数据库表,其中的日期以以下格式存储

在此处输入图片说明

I need to compare the current system date with the date stored in the db. 我需要将当前系统日期与数据库中存储的日期进行比较。 I get the system date using DateTime.Now 我使用DateTime.Now获取系统日期

But since these 2 dates are in different formatting... how can i convert and compare so that i can select only the required values. 但是由于这两个日期的格式不同...我该如何转换和比较,以便我只能选择所需的值。

Try this: Get only date from system datetime and compare with database date column value 尝试以下操作: 仅从系统日期时间获取日期 ,并与数据库日期列值进行比较

You also get only date from database 您还只能从数据库中获取日期

//Database
SELECT CONVERT(date, Transaction_Date) FROM TableName

//Application
if(DateTime.Now.Date==databasedate)

If your database column is datetime format then after pull out data and store in a datetime type variable both will be a C# DateTime instance, So you can compare them without any format specification 如果您的数据库列是datetime格式,那么在提取数据并将其存储在datetime类型变量中之后,两者都将是C#DateTime实例,因此您可以比较它们而无需任何格式说明

DateTime date1 = DateTimeFromDb;
DateTime date2 = DateTime.Now;
int result = DateTime.Compare(date1, date2);

if(result == 0 )
  Console.WriteLine("Same")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM