Format DateTime Data Display In Grid View - asp.net

I am trying to retrieve DateTime data type data from database in Asp.Net. Here is the code:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
However, the problem is the date I retrieved display in such format : 26/11/2013 12.00 AM . I just wanted to display the date but not the time. I'd tried to do this:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
string dateStr = date.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
However, it does not works in my grid view. I wonder why is it so?
Thanks in advance.

Use the built-in to string function for jsut the Date value...
DateTime.ToShortDateString();

Select the corect type from database, so in this case datetime. Then you get a DateTime in your DataTable which you can cast with the DataRow's Field extension method:
DateTime date = dr.Field<DateTime>("packingDate");
If you only want to diplay the date:
string dateOnly = dr.Field<DateTime>("packingDate").ToShortDateString();
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("d");
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

You could format on the GridView control, for sample:
<asp:BoundField DataField="packingDate" HeaderText="PackingDate"
DataFormatString="{0:dd-M-yyyy}" />

Related

ASP .Net Razor: format substract of 2 DateTime to a string specific format

I've got 1 model.DateTime value (which is I'm getting from Database) and I want to represent model DateTime which is end time, and DateTime.Now as now time.
I want to format subtract of that 2 DateTime-s as a specific String format "dd:hh:mm:ss".
In your ASP .Net view page you can open razor block and you can calculate difference between 2 variables that are DateTime type.
#{
DateTime end = (DateTime)item.close_date_time;
DateTime now = DateTime.Now;
TimeSpan diff = end.Subtract(now);
}
Then you can simply convert TimeSpan diff to string:
#diff.ToString(#"dd\:hh\:mm\:ss")
And by that I've solved time difference and to format it to specific string.
EDITED
Simply you can do this:
DateTime CurrentDateTime = DateTime.Now;
item.duration.Subtract(CurrentDateTime).ToString(#"dd\:hh\:mm\:ss");
Hope this will help

Format date from the following string in asp.net

I'm new to coding in asp.net and I'm struggling to format the date I've managed to pull from our CRM content pages' live date. I've wrote the following code that manages to pull and display the date the page was created:
TheDate = DR["LiveDate"].ToString();
But it formats the date and time like this:
13/11/2012 00:00:00
How can I modify my above code to just display the date like so:
Tuesday, 13 November 2012
Or, just without the time
13/11/2012
Thanks,
I assume that DR is a DataRow, isn't it?
If DR["LiveDate"] is already a DateTime column don't convert it to a localized string via ToString. Just cast it accordingly, for example via Field extension method:
DateTime dt = DR.Field<DateTime>("LiveDate");
If it's not a DateTime but a string you have to parse it to DateTime:
DateTime dt = DateTime.Parse(DR.Field<String>("LiveDate"));
If you want Tuesday, 13 November 2012 as result use:
string result = dt.ToString("dddd, dd MMMM yyyy", CultureInfo.InvariantCulture);
If you want 13/11/2012 use:
string result = dt.ToShortDateString();
or
string result = dt.ToString("d");
if your culture uses / as separator
otherwise
string result = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Further informations: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
for your second question, try
TheDate = DR["LiveDate"].ToString("dd/MM/yyyy")

How to get only the date excluding time in asp.net c#

How to get only the date excluding time in asp.net c#. I want only the date to be given as input to search like eg 3/11/2013
You can use DateTime.Date to get only date part of DateTime object
DateTime dateOnly = date1.Date;
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
If you have the Date in string and want to convert it to DateTime object first then you can use DateTime.ParseExact
result = DateTime.ParseExact("3/11/2013", "d/MM/yyyy", CultureInfo.InvariantCulture);
try to use this:
#{
DateTime dd=DateTiem.Now;
string date=dd.toString("dd/MM/yyyy");
}
Now to view just:
#date
DateTime dt = your_Dt.Date;
OR
You can format it into whatever format you want as below:
dt.Tostring("MM/dd/yyyy");
OR
You can convert the valued to shortdate as:
Convert.ToDateTime(your_Dt).ToShortDateString();
I saved the database as datetime in MS-SQL 2014, but when i need to only in date i do as below .... in cshtml
#foreach (var item in Model.dbModelLst)
{
<tr>
<td>#item.ChargeFrequency</td>
<td>#item.Charge</td>
<td>#item.FromDate.ToShortDateString().ToString()</td>
<td>#item.ToDate.ToShortDateString().ToString()</td>
<td>#item.Inv_SerialNo.SerialNo</td>
<td>#item.IncludeLic</td>
<td>#item.Status</td>
.....
.....
</tr>
}
Where dbModelist contain List of Model (IEnumerable)... I Solve this way. Thank you.

String is not valid Datetime

I am writing this code the text Change event but it is showing the error that is string is not valid.
DateTime ts=Convert.ToDateTime(Joiningdate.Text);
DateTime dt1=ts.AddMonths(6);
txtcd.Text = dt1.ToShortDateString();
The DataTime format does not match the format you need to convert string to DateTime object. You can use DateTime.ParseExact() to give you format you date you have.
Assuming you have format dd/MM/YY for the textbox
DateTime ts= DateTime.ParseExact(Joiningdate.Text, "dd/MM/yy", CultureInfo.InvariantCulture);
DateTime dt1=ts.AddMonths(6);
txtcd.Text = dt1.ToShortDateString();
The problem is with the format of Joiningdate.Text
You will have to tell that in what format your date time is.
If your date time is in format use
DateTime ts= DateTime.ParseExact(Joiningdate.Text, "ddMMyyyy",
CultureInfo.InvariantCulture);
DateTime dt1=ts.AddMonths(6);
nd then you can convert back to any format you want
ts.ToString("yyyyMMdd");
You can go through this link
Convert DateTime to string format("yyyyMMdd")

ASP.NET how to convert string to Date without forward slashes

Just wondering how I can convert the following string into a datetime 111222, at the moment it is telling me that this is not a valid datetime value..
You can use the DateTime.ParseExact method and supply the format your input is in.
Edit - added code:
DateTime dt = DateTime.ParseExact("111222", "yyMMdd", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString());

Resources