© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
2 replies
Mek

✅ Converting string to DateTimeOffset

public List<EntryModel> LoadEntries(User user)
{
    using SQLiteConnection conn = new(_dbFile);
    using SQLiteCommand cmd = conn.CreateCommand();
    SQLiteDataReader reader;

    conn.Open();

    cmd.CommandText = @"SELECT * FROM entries WHERE Username=$un";
    cmd.Parameters.AddWithValue("$un", user.Username);

    try
    {
        reader = cmd.ExecuteReader();

        List<EntryModel> allEntries = new();

        while (reader.Read())
        {
            string format = "MM/dd/yyyy";
            CultureInfo culture = CultureInfo.InvariantCulture;
            DateTimeStyles style = DateTimeStyles.None;

            int iden = Convert.ToInt32(reader["Id"].ToString());
            string username = reader["Username"].ToString();
            string title = reader["Title"].ToString();
            DateTimeOffset date;

            if (!DateTimeOffset.TryParseExact(reader["Date"].ToString(), format, culture, style, out date)
            {
                date = DateTimeOffset.Now;
                date.ToString("MM/dd/yyyy");
            }
            else
            {
                // convert correctly formatted string to a DateTimeOffset object
                string savedDate = reader["Date"].ToString();
                var splitDate = savedDate.Split("/");
                date = new DateTimeOffset(Convert.ToInt32(splitDate[0]), 1, 1).AddDays(Convert.ToInt32(date[1])-1);
            }
        }
    }
}
public List<EntryModel> LoadEntries(User user)
{
    using SQLiteConnection conn = new(_dbFile);
    using SQLiteCommand cmd = conn.CreateCommand();
    SQLiteDataReader reader;

    conn.Open();

    cmd.CommandText = @"SELECT * FROM entries WHERE Username=$un";
    cmd.Parameters.AddWithValue("$un", user.Username);

    try
    {
        reader = cmd.ExecuteReader();

        List<EntryModel> allEntries = new();

        while (reader.Read())
        {
            string format = "MM/dd/yyyy";
            CultureInfo culture = CultureInfo.InvariantCulture;
            DateTimeStyles style = DateTimeStyles.None;

            int iden = Convert.ToInt32(reader["Id"].ToString());
            string username = reader["Username"].ToString();
            string title = reader["Title"].ToString();
            DateTimeOffset date;

            if (!DateTimeOffset.TryParseExact(reader["Date"].ToString(), format, culture, style, out date)
            {
                date = DateTimeOffset.Now;
                date.ToString("MM/dd/yyyy");
            }
            else
            {
                // convert correctly formatted string to a DateTimeOffset object
                string savedDate = reader["Date"].ToString();
                var splitDate = savedDate.Split("/");
                date = new DateTimeOffset(Convert.ToInt32(splitDate[0]), 1, 1).AddDays(Convert.ToInt32(date[1])-1);
            }
        }
    }
}

I know it's rather simple to do, but I just can't figure it out. In my database file, I'm pulling all the entries from the database and I'm trying to add the value to the list as the appropriate types that they are, however, I just cannot figure it out. The model is setup for the date to be a DateTimeOffset type. It's because the helper function that I'm going to write for when the user creates a new entry is to take the datetime widgets input and convert it to a MM/dd/yyyy format and as a string before saving to the database
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Converting String To DateTimeOffset & Getting Average Days To Goal
C#CC# / help
3y ago
❔ error when converting string to long
C#CC# / help
3y ago