I have an application that use sqlite as local database, but I have problems with the format of the dates.
I am using System.Data.Sqlite as database provider and I create my edmx, that is created from the sqlite database.
When I make a query to the database, in my entity class that has some date field, I can see that the dateTime property has the correct format, in my case, dd mm yyyy and 18:00 (the hour is of 24 hours, not 12pm for example).
However, when I do the binding (I am using WPF and MVVM) to the dataGrid, in the columns of date the format of the date is mm dd yyyy 12pm (the hour is not of 24).
Why?, if I receive the data in the correct format, because the entity object has de date in the correct format I see the information in other format in the dataGrid? When I use SQL Server as dataBase, I don't have this problem, the format is correct in the dataGrid.
My windows is configurated to ES (Spain).
Thanks.
Use can use this xaml
Binding="{Binding MyColumn, StringFormat=MM/dd/yyyy}"
Use this code in DataGridTextColumn tag.
This is a sample code.
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn
Header="MyHeader"
Width="Size"
Binding="{Binding MyDateColumn, StringFormat=MM/dd/yyyy}" />
</sdk:DataGrid.Columns>
I would guess that the problem is with the DateTime.Kind property. Dates can be Local, UTC, or unknown. Check that the date is restoring from the database with the same Kind-ness, and if not, adjust your Sqlite connection parameters to use the Kind you wish to work with.
The proper way to handle this, in my opinion, is to always store dates/times as UTC in the database and always query against the database using equivalent UTC representation. You can set the DateTime kind to UTC in the Sqlite connection settings. You can ensure that your objects have local times in your property setter if you want the objects to always represent times that way, like so:
public DateTime YourDateTimeProperty
{
get { return _dateTime; }
set { _dateTime = value.ToLocalTime(); }
}
Which should ensure that times stay local after being loaded from the database.
Related
I have an entity with a DateTimeOffset (since .NET doesn't have a Date class) that is supposed to store a date (no time).
The problem is that, when I set the date to, for example, "2017-9-1" in the database it's saved as "2017-08-31 22:00:00+00" (2 hours less)
I think it applies the offset of my time zone to UTC.
I would like to store like to store "2017-9-1" in the database. The first thing I thought is to add 2 hours to every DateTimeOffet, but it feels bogus.
Is there a better way to work with dates than this?
I'm not quite sure, but add "0:" before the date on the format string, I think that should put it to your current time, otherwise it will default to UTC.
Like this:
[DisplayFormat(DataFormatString = "{0:yyyy/dd/MM}")]
If you are using MS Sql, you can use the Date variable to store just date. You can annotate the entities DateTimeOffset property database type.
[Column(TypeName="date")]
I have a weird problem with sql server 2008. I am trying to save date with dd/MM/yyyy format into sql server 2008, But after inserting date it automatically get converted into MM/dd/yyyy.
Note : my computer clock format is dd/MM/yyyy
My report viewer date text box properties
enter image description here
Date from my database table
enter image description here
my c# code
lbldate.Text = DateTime.Today.ToShortDateString();
date on report
05/04/2017
SQL Server is the Data Layer and as such there is no formatting available; it stores a date as a 4 byte number which is relative to days with 0 = 01/01/1900.
The Application Layer DateTime type is generally an ODBC Canonical representation which basically looks like a class with integer properties for each component (year, month, date, hours, minutes, seconds, milliseconds).
The Presentation Layer is what you actually see, and that is where you should be concerned. When your application calls the ToShortDateString() method, it is calling the display format from the threads current culture, which may or may not reflect the systems settings for Region & Language or Date & Time.
Solution number one is to set the threads current culture, but this would just go to that particular cultures standard display
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Solution number 2 is to just use a custom DateTime format string
lbldate.Text = DateTime.Today.ToString("dd/MM/yyyy");
I would not say this is a "problem" so to speak. This is how SQL handles dates. Your computer clock format is not relevant. To change the format, use CONVERT in your queries. Ex:
SELECT CONVERT(varchar, GETDATE(), 103)
Results: 04/05/2017
SELECT CONVERT(varchar, GETDATE(), 101)
Results: 05/04/2017
The codepage arguments are listed here: https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
edit per your new update: Your C# should look something like this:
DateTime.Now.ToString("dd/mm/yyyy")
What you're seeing is how the query tool presents results and has nothing to do with how Sql Server stored the data. Sql Server actually stores dates in a binary (not readable) format.
This is a good thing. Let Sql Server store dates (and other data) how it wants. You only need to worry about how you show the data to users after you retrieve it, and most of the time the best place to worry about that formatting isn't even in the server at all, but in your client language.
I wanted to save only date in my database. There is a table which holds dates in database and the column which holds date in type of "Date". Now I want to store date from UI,so I placed WPF DatePicker in UI which allows to select date, but whenever I try to get the data from datepicker it shows the date and time.But I want just the dates to be stored in database.
This is the thing i am doing. It is demo code by the way. Can t upload original code. But this explains the thing. you can see in the message box, it shows 14-10-2015 00:00:00 , i want this zeros to be removed.
The dateTime Picker has a property DisplayDate of Type DateTime. This type contains date and time information.
Just use picker.DisplayDate.Date this returns a DateTime value with the TimeOfDay component set to 00:00.00
Edit
Usually you use an SQL Statement to insert or update values in the database. You should use a parametrized SQL statement with an parameter of type DateTime. The SQL API will take care of the conversion form DateTime (.Net type) to your SQL Date type and strip all time information away. It is a good idea to set the time component to 00:00:00 however to avoid any strange "roundings".
Use ToShortDateString() at the end something like:
var date = datePicker.SelectedDate.Value.Date.ToShortDateString();
MessageBox.Show(date.ToString());
In my SQL database, I have BeginDate column of type date (so I want to get only day,month,year).
I add data into database in C#.
I look at Table_Data and everything is fine (for example BeginDate = 05/04/2014).
But when I get this data from database in C# with
string a= dt.Rows[i]["BeginDate"].ToString()
result is: a= 05/04/2014 12:00:00 AM
Why?
I can remove hour in string a with any methods. But why? I want to get only 05/04/2014 from database.
This is a display problem, not a data problem. Use the Formatting features of DateTime.ToString() to display the date in the format that you want.
If the data is coming out of the database in string form, use DateTime.Parse on it first.
Reference
Standard Date and Time Formats
Custom Date and Time Formats
I had problem in inserting datetime values to sqlite database in C#. Later i found a way to do it. Now i can insert DateTime values by formatting them like this
data.Add("sayacTarihZamani", string.Format("{0:u}", MydateTimeData));
I can see that data is inserted to the database table correctly using a sqlite admin tool.
When i query this data the values which come are wrong. The values are 3 hours later then its normal value.
For example in admin tool i see 30.03.2011 16:00:00 value but in C# grid the value is 30.03.2011 19:00:00 ..
There is one exception to this. I manually entered a row using the admin tool. It's date value is
30.03.2011 11:30:00 in datagrid it is still 30.03.2011 11:30:00 (true)
How can i correctly insert and query the datetime data using sqlite and C#.
Thank you for your time,
Ferda
When you use the u specifier, your DateTime is automatically converted to universal time. From the docs (emphasis mine):
The "U" standard format specifier represents a custom date and time
format string that is defined by a specified culture's
DateTimeFormatInfo.FullDateTimePattern property. The pattern is the
same as the "F" pattern. However, the DateTime value is automatically
converted to UTC before it is formatted.
Try using the F pattern instead if you want to store the local time.
data.Add("sayacTarihZamani", string.Format("{0:yyyy-MM-dd HH:mm:ss}", mydatetime));
worked all right.
Thank you for your help
Ferda