问题描述
我想从一个字符串
$ 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
?
否,这是不可能的。
每个实例必须具有 DateTime
部分。您不能使用值。
不采取 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
内部为当前实例。
如果可以这样做,你不会需要那个字符串,你不觉得吗?
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
?
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.
If you could do that, you wouldn't need that string, don't you think?
这篇关于从字符串新的DateTimeOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!