开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 3671|回复: 3

[求助] 谁能给个C#中字符串转日期的例子

[复制链接]

0

精华

0

贡献

0

赞扬

帖子
6
软币
62
在线时间
0 小时
注册时间
2013-12-18
发表于 2013-12-18 12:34:30 | 显示全部楼层 |阅读模式
谁能给个C#中字符串转日期的例子
回复

使用道具 举报

0

精华

94

贡献

20

赞扬

帖子
56
软币
970
在线时间
209 小时
注册时间
2013-12-6
发表于 2013-12-18 12:44:34 | 显示全部楼层
  1. DateTime dt = Convert.ToDateTime("2013-12-18 12:38:00");
复制代码

这样不行吗?
回复

使用道具 举报

0

精华

304

贡献

390

赞扬

帖子
185
软币
1927
在线时间
269 小时
注册时间
2013-8-28
发表于 2013-12-27 11:27:00 | 显示全部楼层
可用
DateTime.TryParse()方法:
public static bool TryParse(
        string s,
        out DateTime result
)


DateTime..::.TryParseExact 方法:
public static bool TryParseExact(
        string s,
        string format,
        IFormatProvider provider,
        DateTimeStyles style,
        out DateTime result
)
回复

使用道具 举报

0

精华

74

贡献

722

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
183
软币
3861
在线时间
327 小时
注册时间
2013-6-7
发表于 2013-12-27 14:25:49 | 显示全部楼层
使用 DateTime的静态方法 ParseExact或是TryParseExcat的可以指定一个格式化数组的重载函数, 格式串数组你可以列举出可能的所有格式串


  1. string dateStr = "2013年2月12日";
  2. DateTime dt = DateTime.ParseExact(dateStr, new string[] {"yyyy年MM月dd日","yyyy年M月d日" }, null, DateTimeStyles.None);
复制代码
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-30 01:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表