0
Follow
2
View

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()
  list.Add(live.Date)
 //intlist.Add(int.Parse(live.Date))
}

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.

 [Table("Live")]
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

表格中的公式在某个日期后断开IMPORTRANGE链接

调用未知方法:hasMany()

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

安卓Android Studio 虚拟机调试

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

如何创建一个删除功能的vanillajs待办事项列表应用程序?

ansible loop中未定义变量[closed]

访问django数据库对象的元素

Android 13 通话默认只有免提

EF中SelectMany后的GroupBy会导致SQL错误