I'm querying a datetime
(dd/mm/YYYY hh:mm:ss) value from a database and inserting it in a list like this:
ord.invoiceDate = dt.Rows[i]["invoicedate"].ToString();
How can I convert this string to a custom format like dd-MM-YYYY
? I don't want the hours minutes and seconds.
尝试这个
ord.invoiceDate = ((DateTime)dt.Rows[i]["invoicedate"]).ToString("dd-MM-yyyy");
If you know the Format of date time string, then you can use DateTime.ParseExact
method as below to convert it to DateTime. If you not sure about the format then use DateTime.TryParseExact
it will not raise exception on fail to convert but you will get null value as result.
var invoiceDate = DateTime.ParseExact(dt.Rows[i]["invoicedate"],
"dd/mm/YYYY hh:mm:ss", CultureInfo.InvariantCulture);
After you got the result you can convert to string by giving format as below
ord.invoiceDate = invoiceDate.ToString("dd-MM-yyyy");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.