要格式化的字符串
“新日期(2009,0,1)”
格式化后的字符串
"'01-Jan-2009'“
发布于 2010-05-14 21:51:15
您应该使用DateTime结构:
DateTime date = DateTime.Parse(something);
string str = date.ToString("dd-MMM-yyyy");在您的示例中,您应该使用正则表达式来解析字符串中的值,如下所示:
static readonly Regex parser = new Regex(@"new Date\((\d{4}),\s*(\d\d?),\s*(\d\d?)\)");
Match match = parser.Match("new Date(2009,0,1)");
DateTime date = new DateTime(
int.Parse(match.Groups[1].Value),
int.Parse(match.Groups[2].Value) + 1,
int.Parse(match.Groups[3].Value)
);
string str = date.ToString("dd-MMM-yyyy");(已测试)
发布于 2010-05-14 21:52:35
您不能为此使用正则表达式-您只能处理现有的文本。添加新字符(Jan而不是1(或0??))是不可能的。
你能做的就是匹配@"new Date\((\d+),(\+d),(\d+)\)。然后,在Match对象的Group1、Group2和Group3中有三个包含2009、0和1的字符串。
https://stackoverflow.com/questions/2834592
复制相似问题