Retrieve a string from database and compare with date

与世无争的帅哥 提交于 2021-02-11 12:05:17

问题


I have a field in database stored as string and i need to convert it back to datetime and then compare if it equals to date. Below is the current implementation I have presently.

var date = DateTime.UtcNow ;
        var zone = TimeZoneInfo.FindSystemTimeZoneById("W. Central Africa Standard Time");           
        DateTime currentTime = TimeZoneInfo.ConvertTimeFromUtc(date, zone);
        var loanDate = currentTime.Date.ToString("dd/MM/yyyy").Replace("-", "/");


if (DateTime.ParseExact(firstRepay, "dd/MM/yyyy", CultureInfo.InvariantCulture).Date.ToString("dd/MM/yyyy").Replace("-", "/") == WATTime.Date.ToString("dd/MM/yyyy").Replace("-", "/"))
  { // Do this
}

note that the firstRepay is in the format 06/02/2021 in the database but it might be 06-02/2021 depending on server format.


回答1:


Assuming loanDate and dbDate are in the same format "dd/MM/yyyy" and the dbDate also can be in "dd-MM/yyyy" format, you can try this code

var dateFromDb="01-02/2021"; // 1 Feb 2021
var loanDate="06/02/2020";  // 6 Feb 2020
var dbDate =  dateFromDb.Replace("-", "/"); 
    
var dbDateTime = DateTime.ParseExact(dbDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

var loanDateTime= DateTime.ParseExact(loanDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
     
var diffDateDays=(dbDateTime-loanDateTime).Days; // = 361
    
//or you can use it this way:
    
if ((dbDateTime-loanDateTime).Days > 0) //.... then

If dates are in a different string format you just have to change string "dd/MM/yyyy" to "MM/dd/yyyy" for example for this date, but the algorithm will be still the same.




回答2:


Please try to use like below.

string dateString = "21-Jan-2021"; 
DateTime otherDate=new DateTime(2021,3,3); 
// Convert a null string.  
DateTime mydateTime;  
if(DateTime.TryParse(dateString, out mydateTime))
{
  //dateString is converted to DateTime in mydateTime
   if(mydateTime==otherDate)//Check with exact Date and time
   {
    //DB date and other date is equal
   }
   if(mydateTime.Date==otherDate.Date)//Check with Only date
   {
    //DB date and other date is equal
   }
}

Note: Learn different way to convert string to DateTime How to Convert String To DateTime in C#.




回答3:


Which database you are using, you should directly parse date in your SQL query.

Below is sample for SQL Server

SELECT CAST('06/02/2021' as date) as MyDate
SELECT CAST(REPLACE('06-02/2021','-','/') as date) as MyDate
SELECT CAST(REPLACE('06-02-2021','-','/') as date) as MyDate
SELECT CAST(REPLACE('06-February-2021','-','/') as date) as MyDate

All these will return date which will be directly casted as DateTime in C#. You can parse all your rows using SQL statement at once.

If you have space or some extra chars that can be managed by trim/ regex.



来源:https://stackoverflow.com/questions/66080060/retrieve-a-string-from-database-and-compare-with-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!