从字符串新的DateTimeOffset [英] New DateTimeOffset from string

查看:127
本文介绍了从字符串新的DateTimeOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个字符串

$ b创建一个新的 DateTimeOffset offset = -5
$ b

我做:

  string dt =11082016; 
DateTime date = DateTime.ParseExact(dt,MMddyyyy,
CultureInfo.InvariantCulture,
DateTimeStyles.None);
DateTimeOffset dto = new DateTimeOffset(date,TimeSpan.FromHours(-5));

是否可以直接创建一个 DateTimeOffset 不通过 DateTime

解决方案


是否可以直接创建一个DateTimeOffset,而不必通过
DateTime?


否,这是不可能的。

每个 DateTimeOffset 实例必须具有 DateTime 部分。您不能使用 DateTimeOffset 实例rel =nofollow noreferrer> UTC偏移量值。



当然,它有一些构造函数不采取 DateTime 作为参数直接喜欢;




  • DateTimeOffset(Int32,Int32,Int32,Int32,Int32,Int32 ,Int32,Calendar,TimeSpan)

  • DateTimeOffset(Int32,Int32,Int32,Int32,Int32,Int32,Int32,TimeSpan)

  • DateTimeOffset ,Int32,Int32,Int32,Int32,Int32,TimeSpan)

  • DateTimeOffset(Int64,TimeSpan)



但是,这些 Int32 Int64 值仍然生成 Datetime 内部为当前实例 .DateTime 属性


我想创建一个新的DateTimeOffset,offset = -5从一个字符串


如果可以这样做,你不会需要那个字符串,你不觉得吗?


I want to create a new DateTimeOffset with offset = -5 from a string

I do :

string dt = "11082016";    
DateTime date = DateTime.ParseExact(dt, "MMddyyyy", 
                                    CultureInfo.InvariantCulture, 
                                    DateTimeStyles.None);    
DateTimeOffset dto = new DateTimeOffset(date, TimeSpan.FromHours(-5));

Is it possible to create directly a DateTimeOffset without passing by DateTime ?

解决方案

Is it possible to create directly a DateTimeOffset without passing by DateTime ?

No, that's not possible.

Every DateTimeOffset instance has to have it's DateTime part. You can't create a DateTimeOffset instance just with a UTC Offset value.

Of course it has some constructors that doesn't take DateTime as a parameter directly like;

  • DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)
  • DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)
  • DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)
  • DateTimeOffset(Int64, TimeSpan)

But those Int32 and Int64 values are still generate a Datetime internally for current instance .DateTime property.

I want to create a new DateTimeOffset with offset = -5 from a string

If you could do that, you wouldn't need that string, don't you think?

这篇关于从字符串新的DateTimeOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆