Format A Date With Offset

让人想犯罪 __ 提交于 2019-12-25 06:08:36

问题


I am attempting to alter a string DateTime value with Offset. This is the procedure I have attempted, but in the end, both datetime & datetime1 print their initial values. My desired output is to format datetime1 to the proper Offset so that it mirrors datetime

01/10/2016 5:18 PM
01/10/2016 5:18 PM-05:00

string datetime = "2017-01-10T17:18:00-05:00";
string datetime1 = "1/10/2016 3:18:00 PM";

DateTimeOffset dateTimeOffset = DateTimeOffset.Parse(datetime);
TimeSpan tspan = dateTimeOffset.Offset;
DateTimeOffset alteredDate = new     DateTimeOffset(Convert.ToDateTime(datetime1)).ToOffset(tspan);

UAB = Convert.ToString(DateTimeOffset.Parse(alteredDate.ToString()));

Console.WriteLine(datetime);
Console.WriteLine(UAB);
Console.ReadLine();

EDIT
When stepping through my code, I noticed that tpsan holds a value of -05:00 could the - sign be what is causing the code to not convert properly?


回答1:


The DateTimeOffset object you receive is already adjusted for timezone.

string output = "";

// Parsing with explicit timezones
var withZeroOffset = DateTimeOffset.Parse("2017-01-10T17:18:00-00:00"); // parsed as UTC
var withOffset = DateTimeOffset.Parse("2017-01-10T17:18:00-05:00"); // Parsed as specific timezone
var withoutOffset = DateTimeOffset.Parse("2017-01-10T17:18:00"); // Parsed as my timezone
output += "All Times:\n" + withZeroOffset + "\n" + withOffset + "\n" + withoutOffset + "\n\n";

// Modifying timezones
var inputUtc = DateTimeOffset.Parse("2017-01-10T17:18:00Z").ToOffset(TimeSpan.Zero);
output += "Time UTC: " + inputUtc + "\n";
var minusFive = inputUtc.ToOffset(TimeSpan.FromHours(-5));
output += "Time @ -5:00: " + minusFive + "\n";
var myOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
var myTime = inputUtc.ToOffset(myOffset);
output += "Time in my timezone: (" + myOffset.TotalHours + ":00): " + myTime + "\n";

Console.WriteLine(output);

On my machine, with my timezone, I get this output:

All Times:
1/10/2017 5:18:00 PM +00:00
1/10/2017 5:18:00 PM -05:00
1/10/2017 5:18:00 PM -08:00

Time UTC: 1/11/2017 1:18:00 AM +00:00
Time @ -5:00: 1/10/2017 8:18:00 PM -05:00
Time in my timezone: (-8:00): 1/10/2017 5:18:00 PM -08:00

I am assuming your explicit offset matches your actual timezone, which is why you see the same answer twice. To explicitly change the timezone of a DateTimeOffset object, use .ToOffset().




回答2:


Use a different constructor:

DateTimeOffset alteredDate = 
    new DateTimeOffset( Convert.ToDateTime( datetime1 ), tspan );

Here is the documentation:

//
// Summary:
//     Initializes a new instance of the System.DateTimeOffset structure using the specified
//     System.DateTime value and offset.
//
// Parameters:
//   dateTime:
//     A date and time.
//
//   offset:
//     The time's offset from Coordinated Universal Time (UTC).
//
// Exceptions:
//   T:System.ArgumentException:
//     dateTime.Kind equals System.DateTimeKind.Utc and offset does not equal zero.-or-dateTime.Kind
//     equals System.DateTimeKind.Local and offset does not equal the offset of the
//     system's local time zone.-or-offset is not specified in whole minutes.
//
//   T:System.ArgumentOutOfRangeException:
//     offset is less than -14 hours or greater than 14 hours.-or-System.DateTimeOffset.UtcDateTime
//     is less than System.DateTimeOffset.MinValue or greater than System.DateTimeOffset.MaxValue.
public DateTimeOffset(DateTime dateTime, TimeSpan offset);


来源:https://stackoverflow.com/questions/41774124/format-a-date-with-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!