【问题标题】:Getting date in correct formatted for with proper timezone, among list of strings in an a array在数组中的字符串列表中,使用正确的时区获取正确格式的日期
【发布时间】: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


【解决方案1】:

如果需要更正DateTime的时区,可以使用TimezoneInfo.ConvertTime()

string inputString = "2016-08-31T18:30:00.000Z";

DateTime dDate;

if (DateTime.TryParse(inputString, out dDate))
{
    DateTime correctedDateTime = TimeZoneInfo.ConvertTime(dDate, TimeZoneInfo.Local);

    // write this here back into the array using your format
    Console.WriteLine(correctedDateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
}
else
{
    Console.WriteLine("Invalid"); // <-- Control flow goes here
}

如需进一步参考,请查看此post。这个答案的灵感来自它使用TimeZoneInfo

【讨论】:

  • 你能不能让它像,转换成gmt+5.30时间格式?
  • 总是 +5.30 吗?你的数组中的字符串总是来自同一个时区吗?您是否也尝试过@Enigmativity 的答案?
【解决方案2】:
     DateTime dDate;

在 foreach 旁执行此操作

        if (DateTime.TryParse(answerString, out dDate))
        {
            DateTime enteredDate = DateTime.Parse(answerString);
            var Date = enteredDate.ToString("dd/MM/yyyy");
            answerString = Date;
           Console.WriteLine(answerString);
        }
    else{
    //operation
        }

感谢望珠

【讨论】:

    【解决方案3】:

    尝试使用DateTimeOffset 而不是DateTime,因为它是为处理时区而构建的。

    代码如下:

    string inputString = "2016-08-31T18:30:00.000Z";
    
    DateTimeOffset enteredDate = DateTimeOffset.Parse(inputString);
    Console.WriteLine(enteredDate);
    
    DateTimeOffset dtx = enteredDate.ToLocalTime();
    Console.WriteLine(dtx);
    

    这会在 GMT+09:30 为我生成以下内容:

    2016/08/31 18:30:00 +00:00 2016/09/01 04:00:00 +09:30

    要在印度时间得到它,试试这个:

    DateTimeOffset dtx = enteredDate.ToOffset(TimeSpan.FromHours(5.5));
    Console.WriteLine(dtx);
    

    我现在收到2016/09/01 00:00:00 +05:30

    【讨论】:

    • 虽然您给出的答案是可行的,但如果用户应用程序被运送到另一个国家\时区,那么您输入的时间将完全超出。例如,BST、GMT 和 UTC 都是在英国境内引用的时间,或者如果用户处于 CET 时区,然后移动到 PT 时区,则所有数字都将被排除在外。
    • @SimonPrice - 是的,这就是我展示.ToLocalTime() 电话的原因。它在任何用户的时区都可以正常工作。因此,OP 可以在没有 .ToOffset(TimeSpan.FromHours(5.5)) 的情况下简单地使用它。
    • 抱歉,我错过了那句话
    猜你喜欢
    • 2021-12-06
    • 2019-03-04
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多