C
C#10mo ago
Mekasu0124

✅ 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
1 Reply
Mekasu0124
Mekasu0124OP10mo ago
The problem I'm getting with the code in the else statement is 'DateTimeOffset' does not contain a constructor that takes 3 arguments nvm I got it
Want results from more Discord servers?
Add your server