Comparing a Date from an SQLite Database to the current DateTime in C#, Xamarin Forms

cs9_11 注册会员
2023-01-25 01:58

In your code public static void AddLive(string name, string date), you set the type of the date as string.

At first, try to get the data list, because there isn't only one line data in the table.

var stringlist = new List()
// var intlist = new List()
foreach(item in Lists)
  var live = item as Live()

Get the date time now:

string nowTime = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
// and the value of the string will be like "20220322"

And then you can compare them with the "==" such as:

var value = (nowTime == stringlist[i])

You can also compare them in the foreach(item in Lists) and needn't to get the list of the date.

Create a model class as a table. You need to give it a primary key and the attribute named table.

public class Live

    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Date { get; set; }
    public string Place { get; set; }
    public string Time { get; set; }

dian5336 注册会员
2023-01-25 01:58

Sorry I had to delete my previous comments. I was mistaken about SQLite being able to cast the date data as a DateTime object. Therefore, I can throw out a simple parser that will convert the string value of the date data.

If the data is bad then today’s date is returned, however you could return any date. You could use it like…

DateTime dater = GetDTFromString(dateInt.ToString()); 

The parser...

private DateTime GetDTFromString(string dateString) {
  if (dateString.Length >= 8) {
    string year = dateString.Substring(0, 4);
    string month = dateString.Substring(4, 2);
    string day = dateString.Substring(6, 2);
    if (DateTime.TryParse(year + "," + month + "," + day, out DateTime targetDate)) {
      return targetDate;
  return DateTime.Now;

About the Author

Question Info

Publish Time
2023-01-25 01:58
Update Time
2023-01-25 01:58

Related Question



java.lang.Exception:java.lang.ArrayIndexOutOfBoundsException: 4 怎么改呐。

安卓Android Studio 虚拟机调试

在使用C free时报错gcc.exe: cannot specify -o with -c or -S and multiple compilations


ansible loop中未定义变量[closed]


Android 13 通话默认只有免提