Reputation: 327
I'm using ASP.NET to load data from a database to display the date of an item purchased, however it is also showing a time stamp of 12:00:00 AM beside it.
How can I print the value so that it only shows the date?
private void UpdateInventory(int index)
{
DataView inventoryTable = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataRowView row = (DataRowView)inventoryTable[index];
currentInventory.fac_ID = row["fac_ID"].ToString();
currentInventory.inv_Quantity = row["inv_Quantity"].ToString();
currentInventory.inv_Purchase_Date = row["inv_Purchase_Date"].ToString();
lblFacilityID.Text = currentInventory.fac_ID;
lblQuantity.Text = currentInventory.inv_Quantity;
lblPurchaseDate.Text = currentInventory.inv_Purchase_Date;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateInventory(DropDownList1.SelectedIndex);
}
public string fac_ID { get; set; }
public string inv_Quantity { get; set; }
public string inv_Purchase_Date { get; set; }
Upvotes: 1
Views: 119
Reputation: 8938
You could use DateTime.ToString(string)
- for example:
currentInventory.inv_Purchase_Date =
Convert.ToDateTime(row["inv_Purchase_Date"]).ToString("d");
For the string
argument, you can use "d"
, which will yield the same results as DateTime.ToShortDateString()
as MSDN explains; but you also have a lot of flexibility to use other standard and custom short-date format strings.
Note that if your code cannot assume a culture, you should consider conversion options/overloads that leverage IFormatProvider
- for example:
var ci = new CultureInfo("en-US"); // or "ja-JP" etcetera
// ...
currentInventory.inv_Purchase_Date =
DateTime.Parse(row["inv_Purchase_Date"].ToString(), ci).ToString("d", ci);
Upvotes: 0
Reputation: 10456
You can use the ToShortDateString
method:
currentInventory.inv_Purchase_Date = (row["inv_Quantity"] as DateTime).ToShortDateString();
Or use pass a string argument specify a format
currentInventory.inv_Purchase_Date = (row["inv_Quantity"] as DateTime).ToString("d MMMM YYYY");
Upvotes: 1
Reputation: 66449
If you don't need anything too special, but just want to omit the time, you could use this:
lblPurchaseDate.Text
= Convert.ToDateTime(row["inv_Purchase_Date"]).ToShortDateString();
This will display something like:
2/22/2014
Upvotes: 4