Here is my date time format displayed
Dr["DATE"] =========> Jun 27 2014 9:06PM
I want it to display like this one =========> 27.06.2014
Here is my code:
ltrlNotlar.Text = Dr["DATE"].ToString()
How to change first format to the second one?
Thankss
use DateTime to do the conversion for you
var dt = DateTime.Parse(Dr["DATE"].ToString());
ltrlNotlar.Text = dt.ToString("dd.MM.yyyy");
If Dr is a DataRow, you can this
ltrlNotlar.Text = dr.Field<DateTime>("DATE").ToString("dd.MM.yyyy");
I like using .Field because it does the casting for you. However this only works if the sql column is a Date or DateTime (or possibly DateTimeOffset). If you are storing your date as a string in the database, this won't work.
If the sql column DATE is nullable, make sure to change the generic argument to DateTime?.
you could also use something like this:
Convert.ToDateTime(SomeDateTime).ToString("MM.dd.yyyy");
Related
I have problem. I have MySQL database and I use DATE type for my date column. I want to save date without time only.
In database date is saved like this 06/02/1999 for example.
But when I try to take it with dapper
var test = connection.QueryFirstAsync<string>(#"SELECT BirthDate FROM Students");
Then it returns 06/02/1999 00:00:00
How can I fix that? I want string without that time that shouldn't even be there in first place.
Thank you very much for answers
Simple (maybe naive) solution is this one:
string date = "06/02/1999 00:00:00";
DateTime dateTime = DateTime.Parse(date);
DateOnly dateOnly = DateOnly.FromDateTime(dateTime);
Note: DateOnly applies to .NET 6, .NET 7 Preview 6
Why does C# still get the time from SQL if stored procedure only gets the date from a column:
CONVERT (DATE,RecordAdded, 103) as DateCaptured
Result:
I am selecting the date from a datetime column in SQL, then dumping it into a Listview. But I only want the date to display in the listview.
ListViewItem FeedbackTable = new ListViewItem(FeedbackReader["DateCaptured"].ToString());
Because a DateTime is a DateTime, containing both Date and Time part.
And DateTime.ToString() is implementented so it display also the Time part (which is 00:00, as result of your query)
Use the appropriate formatting for DateTime: e.g
myDateTime.ToString("d"); //short date pattern
or
myDateTime.ToString("dd/MM/yyyy"); // your custom display string
Now you have to create a ListViewItem with your correct date format; try this:
ListViewItem FeedbackTable = new ListViewItem(Convert.ToDateTime(FeedbackReader["DateCaptured"]).ToString("d"));
i.e., first you convert FeedbackReader["DateCaptured"]) (which is defined as object) to a DateTime, then apply the correct formatting (with ToString("d");) and then create your listviewItem with the desired string
I have found my solution. Logic actually, but so I learn...
I changed in SQL procedure this:
CONVERT (DATE,RecordAdded, 103) as DateCaptured,
To:
CONVERT (VARCHAR(10),RecordAdded, 103) as DateCaptured,
Now SQL converts it to a string after formatting it, giving me the result I want.
The Result I wanted
You can use the .ToString() for formatting your DateTime as string. Watch this link for more information. In your case this should work:
ListViewItem FeedbackTable = new ListViewItem(FeedbackReader["DateCaptured"].ToString("yyyy-MM-dd"));
Notice you maybe need to convert Convert.ToDateTime(FeedbackReader["DateCaptured"]);
UPDATE:
If you get the field as Date from your Database, it will be DateTime in C#. And DateTime always contains a Time Component. So what you can do is to modify the result from your StoredProcedure. How you can achieve this is described here.
Change the return Type of your StoredProcedure to string, so you can correctly show the information in your ListView:
SELECT LEFT(CONVERT(VARCHAR, #myDateTime, 120), 10);
I have this 21/10/1999 value manually inserted into the SQL database and when I retrieve it like this:
txtDOB.Text = readPatient["pDOB"].ToString();
It displays everything along with the time 12:00:00 AM. My data type for my date column is date, not datetime. So why is it displays the time as well?
((DateTime)readPatient["pDOB"]).ToString("d");
For a list of supported formats see: http://msdn.microsoft.com/es-es/library/zdtaw1bw(v=vs.110).aspx
You may try
txtDOB.Text = Convert.ToDateTime(readPatient["pDOB"]).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Edit: Thanks to comments saw that forgot to cast to datetime
It looks like you got your answer on how to properly format the value to a string without the time portion. But, no one has answered your other question:
So why is it displays the time as well?
This is because of mappings between SQL and .NET. In .NET a SQL "date" is stored in a DateTime object which does have time as well. To see all mappings between SQL and .NET see this: SQL Server Data Type Mappings
you can try this
txtDOB.Text=Convert.toDateTime(readPatient["pDOB"]).ToString("d/MM/yyyy");
You can use the string format parameter of the ToString to achieve any format you like,
like this:
TextBox1.Text =Convert.toDateTime(readPatient["pDOB"]).ToString("d/MM/yyyy");
Click on the datetimepicker then go to properties. There you can format your datetimepicker. Change format to custom (there few selection there) then set custom (provided in the properties menu) table with yy-MM-dd,an
DateTime dt = Convert.ToDateTime(sqdr["invdate"].ToString());
dt.Date; //To get the date only
Does anyone know how to get the day part from a datetime? For example, I have a datetime 2013-11-30 11:53:02.360. I just need the 30 from the datetime. How do I get it using sql and C#?
Update: Maybe I didn't explain it right. Here is the scenario:
We have a record in our table which has start date and end date, for example:
2013-10-03 11:53:02.360
2013-10-08 14:36:01.090
In my web page I have an event that only occurs if the current day is between 3-8, I don't care about the year and month. So if the current date is 2014-01-05, the event will occur.
Now I question is, if I have 2013-09-30 ... as start date in the table, how do I check to make sure the event will still occur? Basically I think I need to check current date is fall into the same rage of the dates defined in the table.
DateTime.Parse("2013-11-30 11:53:02.360").Day
In T-SQL (assuming you're talking about SQL Server when you say SQL):
SELECT DAY(GETDATE())
In C#:
int day = DateTime.Today.Day;
if your date Type is DateTime and contains 2013-11-30 11:53:02.360
Try This:
String Day=dt.ToString("dd"); //it contains Day-> 03
( or )
if your date Type is String and contains 2013-11-30 11:53:02.360
Try This:
String strDate = "2013-11-03 11:53:02.360";
DateTime dt = DateTime.ParseExact(strDate, "yyyy-MM-dd HH:mm:ss.fff",
System.Globalization.CultureInfo.InvariantCulture);
String Day=dt.ToString("dd"); //it contains Day-> 03
I am using ASP.NET3.5 with C# 2008 and I am novice to use LINQ. I have a dataset to which I am fetching table's data that includes a column with type datetime. Now, I want to convert this date in 24 hours format with Date and Hours, Minutes and seconds only. The stored date time is in 12 hours format. I have tried
ds.Tables[0].AsEnumerable().ToList().ForEach(i => i["CreatedDate"] = Convert.ToDateTime(i["CreatedDate"].ToString()).ToString("MM/dd/yyyy HH:mm:ss"));
and then I am passing the dataset to gridview as a datasource.
It is not giving any exception but it is not converting the date into 24 hours format.
Can anyone help please.
Awating for your valuable response..
Thank you,
Dev
"The stored date time is in 12 hours format" No, the string which represents a DateTime is formatted, a DateTime never has a format. Use a DateTime when you store a DateTime, use a string only when you want to display it. If it's already a DateTime in your DataSet you don't need to convert it to a string and back to a DateTime via Convert.ToDateTime(i["CreatedDate"].ToString()).
This code is a mess, sorry:
ds.Tables[0].AsEnumerable().ToList().ForEach(i => i["CreatedDate"] = Convert.ToDateTime(i["CreatedDate"].ToString()).ToString("MM/dd/yyyy HH:mm:ss"));
Why?
You are converting a DataTable to a List<DataRow> although a DataTable is already a in-memory object. That is pointless and inefficient
You are storing a DateTime as string, you can use DateTime as type for a DataColumn in a DataTable, convert it to a string at the very last stage when you want to display the value
You are converting the "string-date" to a datetime, using ToString to format it as string and then you're converting it again to a datetime. This will use the default ToString(with your curentculture) to diplay it which is the reason why you don't get the desired format
So assuming that is's really a string and you don't want/can change it to DataTime, this might work:
var query = ds.Tables[0].AsEnumerable()
.Select(r => Date.Parse(r.Field<String>("CreatedDate")).ToString("MM/dd/yyyy HH:mm:ss"));
gridView1.DataSource = query;
gridView1.DataBind();
Note that you could also use the DataFormatString property of the GridView's BoundField:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="CreatedDate"
HeaderText="Created-Date"
SortExpression="CreatedDate"
DataFormatString="MM/dd/yyyy HH:mm:ss" />
But then you need to store it as/convert it to a DateTime in the table and use that as DataSource.
I hope It will work. You can try this one.
ds.Tables[0].AsEnumerable().ToList().ForEach(i => i["CreatedDate"] = Convert.ToDateTime(i["CreatedDate"].ToString()).ToString("HH:mm:ss"));