Search code examples
c#datetimexamarincompare

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


So I have generated a SQLite database in Xamarin Forms. Each item has a Name and Date. I'm trying to extract the date and set it to a variable so that when each item is loaded, it will convert its date integer to DateTime and compare it to DateTime.Now. I'm not sure how to do this, and I might be missing something very elementary- I'm a complete beginner.

public LivePage()
{
    InitializeComponent();

    LiveService.AddLive("2022年3月19日(土): ILLUSION FORCE presents「ILLUSION FORCE×GAUNTLET LONGSTAGE 'GACHINKO'2MAN GIG」", 20220319);
    LiveService.AddLive("2022年3月20日(日)Phantom Excaliver presents 聖剣フェス」", 202203020);
    LiveService.AddLive("2022年4月2日(土)Bad Company vol.17」", 20220402);
    LiveService.AddLive("2022年4月10日(日)VELL'z FIRE presents", 20220410);
    LiveService.AddLive("2022年4月10日(日)ILLUSION FORCE presents「ILLUSION FORCE×Amiliyah LONG STAGE 2MAN GIG」", 20220410);
    LiveService.AddLive("2022年4月29日(金・祝)渋谷メタル会 presents 渋谷メタル会フェス 2022」", 20220429);
    var Lists = LiveService.db.Table<Live>().ToList();
    MainListView.ItemsSource = Lists;
    int dateInt = Lists[2];
    DateTime dater = Convert.ToDateTime(dateInt);
    int result = DateTime.Compare(dater, DateTime.Now);
    if (result < 0)
    {
        Label.TextDecorations = TextDecorations.Strikethrough;
    }

    ...

How I created the Table

namespace IF2.Models
{
    public class Live
    {
        public string Name { get; set; }
        public string Date { get; set; }
        public string Place { get; set; }
        public string Time { get; set; }
    }
}

Service File

public static SQLiteConnection db;

static void Init()
{
    if (db != null)
        return;

    // Get an absolute path to the database file
    var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Lives.db3");

    db = new SQLiteConnection(databasePath);
    db.DropTable<Live>();
    db.CreateTable<Live>();
}

public static void AddLive(string name, string date)
{
    Init();
 
    var live = new Live
    {
        Name = name,
        Date = date,
        
    };

    var id = db.Insert(live);
}

public static void GetProperties()
{
    Init();
    var Lists = db.Table<Live>().ToList();
}

Solution

  • 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<string>()
    // var intlist = new List<int>()
    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; }
    
    }