支持格式为 yyyy-MM-ddTHH:mm:ss.SSSSSSSZ 的日期时间

Supporting DateTime with format yyyy-MM-ddTHH:mm:ss.SSSSSSSZ

我正在使用 Bot Framework,FormFlow。

当使用 DateTime 类型创建字段时,FormFlow 代码将用户文本输入解析为 DateTime,这在大多数情况下都很好用,但它无法使用以下模板解析输入:2017-05-16T14:32:27.5938714 Z. 我们的用户经常从他们的日志中复制确切的日期,因此接收这个时间模板是我们的机器人的常见场景。有什么可以支持它的吗?

        [Prompt("When did the issue begin (UTC)?")]
        [Template(TemplateUsage.NotUnderstood, "I'm sorry I couldn't understand the date you specified. Try something like '01-20-2017 17:05:03' or '2 days ago'")]
        [Optional]
        public DateTime? StartTime { get; set; }

你读过MSDN DateTime.Parse了吗? 您的格式有示例:

string[] formattedDates = { "2008-09-15T09:30:41.7752486-07:00", 
                               "2008-09-15T09:30:41.7752486Z",  
                               "2008-09-15T09:30:41.7752486",  
                               "2008-09-15T09:30:41.7752486-04:00", 
                               "Mon, 15 Sep 2008 09:30:41 GMT" };
   foreach (string formattedDate in formattedDates)
   {
      Console.WriteLine(formattedDate);
      DateTime roundtripDate = DateTime.Parse(formattedDate, null, 
                                              DateTimeStyles.RoundtripKind);                        
      Console.WriteLine("   With RoundtripKind flag: {0} {1} time.", 
                        roundtripDate, roundtripDate.Kind);                                          
      DateTime noRoundtripDate = DateTime.Parse(formattedDate, null, 
                                                DateTimeStyles.None);
      Console.WriteLine("   Without RoundtripKind flag: {0} {1} time.", 
                         noRoundtripDate, noRoundtripDate.Kind);                                          
   }         
// The example displays the following output:
//       2008-09-15T09:30:41.7752486-07:00
//          With RoundtripKind flag: 9/15/2008 9:30:41 AM Local time.
//          Without RoundtripKind flag: 9/15/2008 9:30:41 AM Local time.
//       2008-09-15T09:30:41.7752486Z
//          With RoundtripKind flag: 9/15/2008 9:30:41 AM Utc time.
//          Without RoundtripKind flag: 9/15/2008 2:30:41 AM Local time.
//       2008-09-15T09:30:41.7752486
//          With RoundtripKind flag: 9/15/2008 9:30:41 AM Unspecified time.
//          Without RoundtripKind flag: 9/15/2008 9:30:41 AM Unspecified time.
//       2008-09-15T09:30:41.7752486-04:00
//          With RoundtripKind flag: 9/15/2008 6:30:41 AM Local time.
//          Without RoundtripKind flag: 9/15/2008 6:30:41 AM Local time.
//       Mon, 15 Sep 2008 09:30:41 GMT
//          With RoundtripKind flag: 9/15/2008 9:30:41 AM Utc time.
//          Without RoundtripKind flag: 9/15/2008 2:30:41 AM Local time.     

我相信MSDN,所以你只需要使用:

DateTime.Parse(formattedDate, null,DateTimeStyles.RoundtripKind)

我 运行 在构建自己的机器人时遇到了同样的问题。我通过在我的表单中引入一个名为 DateString 的新变量解决了这个问题,类型为 String,并省略了原始的 Date 类型为 DateTime 的对象。这允许用户输入任何类型的日期格式。

我创建了一个验证方法来尝试解析两种类型的输入:yyyy-MM-dd 或 dd-MM-yyyy。如果解析成功,我将使用日期填写我所在州的 Date属性 和 return 有效的 ValidationResult。如果无法解析日期(或不符合其他一些标准),我会 return 一个无效的 ValidationResult。致用户