【发布时间】:2016-09-29 07:31:50
【问题描述】:
我有一组数组。
//this is not hard corded, some times array will have multiple no.of strings in date format.
["vishnu","2016-08-31T18:30:00.000Z","1992","banglore"]
我有一个字符串数组,在这些字符串中有一个是日期格式的字符串。
我需要做一个foreach 并且需要检查哪个字符串是日期格式。
如果我们得到日期字符串"2016-08-30T18:30:00.000Z",我需要将其转换为基本日期格式但在正确的时区,这里的日期是2016-08-31,但我需要输出的是
["vishnu","31/8/2016","1992","banglore"]
不是
//check the difference in date!
["vishnu","30/8/2016","1992","banglore"]
目标来自数组,如果string是日期字符串格式,则进行转换。
public static void Main(string[] args)
{
string inputString = "2016-08-31T18:30:00.000Z";
DateTime enteredDate = DateTime.Parse(inputString);
Console.WriteLine(enteredDate);
DateTime dDate;
if (DateTime.TryParse(inputString, out dDate))
{
DateTime dtx = enteredDate.ToLocalTime();
String.Format("{0:d/MM/yyyy}", dDate);
Console.WriteLine(dtx);
}
else
{
Console.WriteLine("Invalid"); // <-- Control flow goes here
}
// DateTime dt = convertedDate.ToLocalTime();
}
【问题讨论】:
-
能否请您发布一些您已经尝试解决此问题的代码。
-
rextester.com/CQCVP23274 我需要输出为 2016 年 1 月 9 日
-
我说得对吗,您需要为当前时区更正日期吗?=!
标签: c# datetime data-conversion