Tag: datetime

DateTime.ParseExact在c#中省略毫秒数?

我想在c#中将字符串转换为Datetime。 String datestring = 2013/03/18 10:54:07.679 我试过DateTime dt=DateTime.ParseExact(datestring, “yyyy/MM/dd HH:mm:ss.fff”,null); 结果是{3/18/2013 10:54:07 AM} 我尝试了DateTime.TryParseExact(datestring,”yyyy/MM/dd HH:mm:ss.fff”,CultureInfo.InvariantCulture,DateTimeStyles.None,out dttt); 结果是{3/18/2013 10:54:07 AM} 在上述两种情况下,它都省略了毫秒(679)。 如何通过保持毫秒来正确地将其转换为日期时间?

C#datetime解析问题

当尝试将日期/时间从字符串转换为DateTime时,我没有得到正确的值。 DateTime testDate = DateTime.ParseExact(“2012-08-10T00:51:14.146Z”, “yyyy-MM-ddTHH:mm:ss.fffZ”, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); 我的结果是2012-08-09 8:51:14 PM。 为什么会被抵消? 我只是想让它成为同样的价值。

为什么DateTime.Now.ToString(“u”)不起作用?

我目前在英国夏令时 ,即UTC +1小时。 我使用以下代码确认我的PC是正确的,它返回true。 System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(Date.Now) 我的问题是为什么UTC格式化程序不能像我期望的那样工作: DateTime.Now.ToString(“u”) 它按照预期以UTC格式返回如下的确切当前系统日期,但结尾的Z( 祖鲁时间 )不是+01:00? 即 2009-05-27 14:21:22Z 不 2009-05-27 14:21:22+01:00 这是正确的function吗?

查找给定范围的缺失日期

我正在尝试在DateTimes集合的两个DateTime变量之间找到缺少的日期。 例如。 Collection 2010-01-01 2010-01-02 2010-01-03 2010-01-05 DateRange 2010-01-01 -> 2010-01-06 会给我一个List 2010-01-04 2010-01-06 我可以想到一些是实现这一点,但没有什么干净和简单 有任何想法吗?

DateTime.ParseExact字符串格式exception

我正在尝试使用以下C#代码将字符串转换为datetime, DateTime dTo = DateTime.ParseExact(dateTo, “mm/dd/yyyy”, CultureInfo.InvariantCulture); 每次我将dateTo传递给2010年1月1日它都会失败,而是需要字符串为01/01/2010。 我应该使用什么字符串格式来支持01/01/2010和1/1/2010?

将DateTime.Now转换为不同的时区

这段代码已经工作了很长时间,但是当我尝试将DateTime.Now作为outageEndDate参数传递时,现在已经破了: public Outage(DateTime outageStartDate, DateTime outageEndDate, Dictionary weeklyHours, string province, string localProvince) { this.outageStartDate = outageStartDate; this.outageEndDate = outageEndDate; this.weeklyHours = weeklyHours; this.province = province; localTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[localProvince]); if (outageStartDate < outageEndDate) { TimeZoneInfo remoteTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[province]); outageStartDate = TimeZoneInfo.ConvertTime(outageStartDate, localTime, remoteTime); outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime, remoteTime); 我在最后一行得到的错误消息是在DateTime参数(outageEndDate)上没有正确设置Kind属性。 我用谷歌搜索并检查了SO的例子,但我真的不明白错误信息。 任何建议表示赞赏。 问候。 编辑 – 确切的错误消息是: The […]

解析包含TimeZone信息的字符串DateTime

我试图将字符串值转换为日期时间值,但我收到System.FormatException错误。 这是因为时区信息是日期字符串的一部分。 有没有能够处理这种转换的方法? string date = “Wed, 04 Jan 2012 20:18:00 EST”; DateTime dt = Convert.ToDateTime(date); Console.WriteLine(dt.ToString());

在C#中处理时间的简单方法?

我目前的做法是这样的 DateTime startHour = new DateTime(1900,1,1,12,25,43); DateTime endHour = new DateTime(1900,1,1,13,45,32); // I need to, say, know if a complete DateTime instance // is later than startHour plus 15 minutes DateTime now = DateTime.Now(); startHour = startHour.addMinutes(15); if (now.CompareTo(new DateTime(now.Year, now.Month, now.Day, startHour.Hour, startHour.Minute, startHour.Second)) > 0) { //I can do something now } 这非常麻烦甚至容易出错。 […]

带有Entity Framework的SQLite DATETIME列

我有一个现有的SQLite数据库,我想在Entity Framework中使用它。 然而,SQLite奇怪的类型系统意味着你甚至可以 create table temp(temp datetime); insert into temp values (‘whatever’); 日期存储为Unix时间整数。 我的模型类是由Visual Studio自动生成的,那么如何告诉代码生成器正确处理这些日期而不是让应用程序抛出 字符串未被识别为有效的DateTime。 启动时的exception?

Datetime.now作为TimeSpan值?

我需要当前的Datetime减去myDate1以秒为单位。 DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00); DateTime myDate2 = DateTime.Now; TimeSpan myDateResult = new TimeSpan(); myDateResult = myDate2 – myDate1; 。 。 我尝试了不同的计算方法但没有效果。 TimeSpan mySpan = new TimeSpan(myDate2.Day, myDate2.Hour, myDate2.Minute, myDate2.Second); 。 它的计算方式并不重要,输出应该只是这些值与秒的差值。