I'm trying to storage into my database ONLY the DATE not DATE AND TIME, only Date, but it always came with the time together.
This is how I do to storage only the date inside of String :
string.Format("{0:dd/MM/yyyy}", DateTime.Now)
And works fine, but now, I want to storage a StringDate to a Date in my SQL Server database. So I did this :
EntityTable...
[Column(TypeName = "date")]
public DateTime? InicialDate { get; set; }
Controller
InicialDate = DateTime.ParseExact("01-12-2016",
"dd/MM/yyyy",
new CultureInfo("pt-BR"));
And inside of my Table, the Date is not at the right format as I expected, I came like this :
2017-12-01 and not, DAY-MONTH-YEAR....
And if I try to make a query and retrieve the date, i came like this :
"InicialDate": "2017-12-01T00:00:00"
I just want to save ONLY the DATE without time!
Correct way is creating your Date type for using whole project. Also there is some Data class already developed: https://github.com/claycephas/csharp-date
If you've already created the table with [Column(TypeName = "date")], please check the schema and data of the table, the type of column InicialDate should be date, which doesn't save the time portion of datetime.
The time portion you saw in the result (i.e. T00:00:00) was appended by EF, when it was cast to DateTime structure.
DateTime is internally just a single number (probably an int/long, representing the number of seconds since the Epoch; not sure about this, though; but it doesn't really matter here) and how it's displayed as a string depends on the format used. DateTime.ParseExact("01-12-2016", "dd/MM/yyyy", new CultureInfo("pt-BR")); parses the date into DateTime internal representation according to the pt-BR rules for displaying date, then when EF is posting this to the db it converts the internal representation to ISO-8601 textual form (because SQL is text-based), then the db engine again converts it into a number form and stores it. When you look in the db, you see it as 2017-12-01, because the tool you use to do this is set up to display dates in the ISO-8601 format.
So, the date is stored correctly, what you seed depends on the format the particular app uses to display dates.
Change DateTime to nvarchar(10) in database if u use DateTime in database 00.00.00 will be automatically assigned by database. So, you are getting time with it while retrieving.
Change column datatype to Varchar issue will be resolved.
Sincerely,
Thiyagu Rajendran
**Please mark the replies as answers if they help and unmark if they don't.
Related
I created a simple website that shows some tables. I created a database from App_Data folder and created some tables. A table has a column of AddDate and its datatype is date.
I want to get only date without time. Currently my website shows the date like this:
1/30/2017 12:00:00 AM
I want to get the date such as Jan, 30 2017.
Here's what I did:
I created a database named TestDB adding from App_Data.
From the database, I added a table and added a column called AddDate.
Its datatype is date.
I selected data from the datatype drop-down of the AddDate column, but I get data with time as shown above.
How do I set date datatype without time in table in Visual Studio?
Update: 6/11
I could get only date by adding {0:d} in the property of the grid view.
For reference:
BoundField.DataFormatString Property
I think this is what you want:
var customDateFormat = yourDate.ToString("MMM, dd yyyy");
You can look into DateTime.ToString method in order to see how you can convert a datetime in the format you are expecting.
There is a slight disconnect between the database layer and the C# code layer logic that we all have to deal with from time to time, in C# DateTime is used for values that are DateOnly and those that carry a time as well.
When storing these values in SQL we can choose to use a Date data type and the time information will simply be omitted.
In your code you can use the .Date property to ignore the time component and return a date value that has a zero (midnight) time value.
So we accept that the runtime data type to use is DateTime now the only concern is how to render the value in the format that you have requested.
The DateTime datatype has a .ToString(string format) method that can be used at the point that you need to visualise the value.
var formattedDate = dateValue.ToString("MMM, dd yyyy");
If you are using Entity Framework or nHibernate ORM Frameworks then they have in built conventions for specifying that the data column data type should be a Date instead of a DateTime, but within your C# code you should use the DateTime data type to hold the date value.
You should use your DateTime like this:
DateTime time = DateTime.Now; // you datetime value
DateTime.Now.ToString("MMMM", CultureInfo.InvariantCulture);
and add using System.Globalization; in the top of of your class
you can also see this link:
How to get full date with full month name like 02 November 2015
We have a DateTime column in a table wherein we store date selected in a dropdownlist from the UI
we are saving it with the default DateTime.Unspecified
So in the database it stored like this
Now the problem is we want to display this in the client as it is, meaning display this datetime as it is regardless of timezones
So currently what we did is just get the datetime value from the database and trim the 00:00:00, but somehow, we are encountering problem that it displays differently in clients in different timezone like for example in -2 Timezone, it displays one day behind.
Any idea how should we approach this? changing the database column to datatimeoffset is not an option for this. We just want to force it to be displayed as it is.
The DateTime class has some property that can adjust the display like :
DateTimeKind.Utc
or
DateTime d = DateRetreived.ToUniversalTime()
In your case, I think you just need to put your dates in UTC format before sending it in the display.
See this question :
How can I format DateTime to web UTC format?
Documentation :
https://msdn.microsoft.com/en-us/library/system.datetime.specifykind(v=vs.110).aspx
I am using C# and I am setting the value of the date time picker to my date from my database. But the format I have in database is different from the format of the Date Time Picker. Is there a way to automatically change the format of my database date to that of the date time picker. I have searched for it but I haven't found it.
I will appreciate your help.
But the format I have in database is different from the format of the Date Time Picke
You are making the mistake of thinking the database has any associated display format when storing a DATETIME - it doesn't - there is an internal representation. All you see is how the date is represented when you view it in some tool.
If your database uses a DATETIME column, as it should, then there is not issue.
What's wrong with this code?
Client c = new Client();
string format = "yyyy/MM/dd HH:mm:ss";
string dateAdded = now.ToString(format);
c.RegistrationDate = DateTime.Parse(dateAdded);
c.RegistrationDate is a dateTime object in the client class and I want it to insert to my database.
However It doesn't convert the freaking date to the format in my mysql database. It always says that string format is incorrect. WHAT have i done wrong???? should I convert my Registration Date to string??? Thanks
**EDIT: Sorry I've forgot to mention. "now" is now = DateTime.Now; it gets the current time of the date and time.
A DateTime doesnt have a format - it's just the date/time. (Whether it's local time, UTC or whatever is a different matter, mind you.)
Firstly, you shouldn't be converting to and from text like you are: that's just a recipe for trouble. Just use:
c.RegistrationDate = now;
... performing any rounding you need to.
You haven't shown how you're trying to insert the value into your database. If you're including the value in the SQL statement directly, that would explain it. You should be using a parameterized SQL statement and passing the value directly in the parameter - no conversion necessary.
If you're already doing that, please show us the code you're trying to use to insert the data, and we'll see what we can do. See the documentation for some examples.
I don't think there is anything wrong with the c# code,except I assume you must be doing
string dateAdded = DateTime.Now.ToString(format);
Otherwise I am not sure what 'now' is.
I want to get records from sql server 2005 in datetime format like this "dd/mm/yyyy h:m:s"
My database date is default datetime format
AttLogId int
...
DownloadLog datetime
I am trying to retrieve the datetime like this:
SELECT AttLogId ,convert(varchar,DownloadLogDate,103) FROM AttLog
ORDER BY DownloadLogDate
but I only get the date and not the time.
I suggest you don't try to get the values in a particular string format. Fetch them as DateTime values and then format them in the .NET code using DateTime.ToString("dd/MM/yyyy H:m:s").
It's almost always worth keeping data in its "natural" data type (date/time here) for as long as possible, only converting to text when you really need to. So your SQL should just be:
SELECT AttLogId, DownloadLogDate FROM AttLog
ORDER BY DownloadLogDate
How you then retrieve the data will depend on how you're talking to SQL (e.g. LINQ to SQL, using SqlDbReader etc). But you should be able to get it as a DateTime and then format it locally. This will make it easier to test, easier to debug, give you more control over cultural aspects (date separators, possibly specifying a standard specifier instead of a custom one, etc).
it is because you are using 103 , so it will give only date .
if you want more format check this :
http://msdn.microsoft.com/en-us/library/ms187928.aspx
According to need, u can try this
SELECT AttLogId , convert(varchar(10),DownloadLogDate,103) +' '+ convert(varchar(8),DownloadLogDate,108) FROM AttLog ORDER BY DownloadLogDate
Note : firstone is for date and second one is for time in H:M:S
hope this help u..