将字符串值从FormCollection转换为日期

时间:2013-06-20 14:33:14

标签: c# asp.net .net parsing

我从new Date(2012,9,3)获得FormCollection["eventDate"] 如何在DateTime变量中获取此值?

2 个答案:

答案 0 :(得分:1)

string input = "new Date(2012,9,3)";

var dateTimeString = input.Split(new[] {'(', ')'}, 
                                 StringSplitOptions.RemoveEmptyEntries)
                          .Last();

var datetime = DateTime.ParseExact(dateTimeString, 
                                   "yyyy,M,d", CultureInfo.InvariantCulture);

答案 1 :(得分:0)

一种方式是这样的:

// new Date(2012,9,3)
var dateVals = s.Substring(9).Replace(")", "").Split(",");
var d = new DateTime(
    Convert.ToInt32(dateVals[0]),
    Convert.ToInt32(dateVals[2]),
    Convert.ToInt32(dateVals[1]));