[英]Date time format in C#
i have a string like "14-Nov-2014" , i want to convert this string to this 14.11.2014 format. 我有一个类似于“ 14-Nov-2014”的字符串,我想将此字符串转换为此14.11.2014格式。 after converting i want to add 14 days to above date.
转换后,我想添加14天以上的日期。 given date is not Datetime format.
给定的日期不是Datetime格式。 Old date="14-Nov-2014" new date=14.11.2014
旧日期=“ 2014年11月14日”新日期= 14.11.2014
is there any way to do in c#? C#有什么办法吗?
Assuming, 假设,
var myString = "14-Nov-2014";
First parse the string, most likely using DateTime.ParseExact
. 首先解析字符串,最有可能使用
DateTime.ParseExact
。 Assuming a few things about the format you have, you could do the following. 假设有关格式的几件事,您可以执行以下操作。 Note you most likely should specify the proper culture for the third argument:
请注意,您很可能应该为第三个参数指定正确的区域性:
var dateTime = DateTime.ParseExact(myString, "dd-MMM-yyyy", null);
Then you can add 14 days to it easily: 然后,您可以轻松地添加14天 :
var dateTime = dateTime.AddDays(14);
To get a new string in a different format just use ToString
with a format string . 要获得其他格式的新字符串,只需将
ToString
与格式字符串一起使用 。 For example: 例如:
var myNewString = dateTime.ToString("d.MM.yyyy");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.