DateTime.Parse()怎么返回毫秒?

hejialin666 2010-04-21 10:32:03
DateTime.Parse(“2010-04-20 05:05:00.135”)
这样得到的结果是 2010-04-20 05:05:00 没有毫秒了,怎么能把这个字符串转换成时间时保存毫秒?
...全文
964 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
GSDN00A 2010-04-21
  • 打赏
  • 举报
回复
楼猪你不知道有MSDN?
Peter200694013 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 hejialin666 的回复:]
引用 11 楼 jimh 的回复:
是显示的问题,datetime里面已经有毫秒数据了,默认情况下,datetime.tostring不显示毫秒,自己注意一点吧。

是不是
DateTime dt = DateTime.Parse(“2010-04-20 05:05:00.135”);
其实dt已经把毫秒保存住了?
[/Quote]
dt中已有毫秒,如下可以输出毫秒
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss:fff"));
gohappy2008 2010-04-21
  • 打赏
  • 举报
回复
DateTime dt = DateTime.Parse("2010-04-21 10:05:00.666");

MessageBox.Show(dt.Millisecond.ToString()); //输出666,毫秒
hejialin666 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 jimh 的回复:]
是显示的问题,datetime里面已经有毫秒数据了,默认情况下,datetime.tostring不显示毫秒,自己注意一点吧。
[/Quote]
是不是
DateTime dt = DateTime.Parse(“2010-04-20 05:05:00.135”);
其实dt已经把毫秒保存住了?
Peter200694013 2010-04-21
  • 打赏
  • 举报
回复
这样就可以了:

DateTime dt = DateTime.Parse("2010-04-20 05:05:00.135");

Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss:fff"));
Peter200694013 2010-04-21
  • 打赏
  • 举报
回复
DateTime dt = DateTime.Parse("2010-04-20 05:05:00.135");

Console.WriteLine(dt.Millisecond); //输出135,毫秒保存了
hejialin666 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tigerleq 的回复:]
最后一个是.吗?
我记得是:吧
[/Quote]
肯定是. 你可以试一下
jimh 2010-04-21
  • 打赏
  • 举报
回复
是显示的问题,datetime里面已经有毫秒数据了,默认情况下,datetime.tostring不显示毫秒,自己注意一点吧。
Peter200694013 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tigerleq 的回复:]
最后一个是.吗?
我记得是:吧
[/Quote]
最后一个是.
nbhx2010 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mohugomohu 的回复:]

dt.ToString("MM/dd/yyyy hh:mm:ss.fff tt")
这个好
[/Quote]

DING
tigerleq 2010-04-21
  • 打赏
  • 举报
回复
最后一个是.吗?
我记得是:吧
mohugomohu 2010-04-21
  • 打赏
  • 举报
回复
dt.ToString("MM/dd/yyyy hh:mm:ss.fff tt")
这个好
hongri520 2010-04-21
  • 打赏
  • 举报
回复
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{

Example.outputBlock = outputBlock;
string dateString = "7/16/2008 8:32:45.126 AM";

try
{
DateTime dateValue = DateTime.Parse(dateString);
DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(dateString);

// Display Millisecond component alone.
outputBlock.Text += String.Format("Millisecond component only: {0}",
dateValue.ToString("fff")) + "\n";
outputBlock.Text += String.Format("Millisecond component only: {0}",
dateOffsetValue.ToString("fff")) + "\n";

// Display Millisecond component with full date and time.
outputBlock.Text += String.Format("Date and Time with Milliseconds: {0}",
dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")) + "\n";
outputBlock.Text += String.Format("Date and Time with Milliseconds: {0}",
dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")) + "\n";

// Append millisecond pattern to current culture's full date time pattern
string fullPattern = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern;
fullPattern = Regex.Replace(fullPattern, "(:ss|:s)", "$1.fff");

// Display Millisecond component with modified full date and time pattern.
outputBlock.Text += String.Format("Modified full date time pattern: {0}",
dateValue.ToString(fullPattern)) + "\n";
outputBlock.Text += String.Format("Modified full date time pattern: {0}",
dateOffsetValue.ToString(fullPattern)) + "\n";
}
catch (FormatException)
{
outputBlock.Text += String.Format("Unable to convert {0} to a date.", dateString) + "\n";
}
}
}


正好我也是用到,看到帮助文档有。呵呵。看看吧……
tigerleq 2010-04-21
  • 打赏
  • 举报
回复
DateTime.Parse(“2010-04-20 05:05:00:135”)

hejialin666 2010-04-21
  • 打赏
  • 举报
回复
难道只能拼了吗?
没有别的一步到位的方法吗?
lpingz 2010-04-21
  • 打赏
  • 举报
回复

DateTime dtt = DateTime.Parse("2010-04-20 05:05:00.135");
int ms = dtt.Millisecond;
mohugomohu 2010-04-21
  • 打赏
  • 举报
回复

DateTime dt = DateTime.Parse("2010-04-20 05:05:00.135");
MessageBox.Show(dt.Millisecond.ToString());

大不了自己拼啊
peng83 2010-04-21
  • 打赏
  • 举报
回复
bangding

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧