c# - Convert datetime to string always defaults to system time zone offset, Need to get the users timezone offset in the result -
i have code user enters datetime , timezone in dates needs converted , format in end date should be. below code.
the code converts datetime timezone user has entered when formatting datetime, date time offset value defaulted systems time zone offset. example when convert date 2014-10-30t08:01:01-06:00(central time) pacific standard time , format it. result 2014-10-30t06:01:01**-06:00** , should 2014-10-30t01:01:01**-08:00** offset value defaulted systems time zone central time.
datetime inputtime; string timezoneid; string outputdateformat; inputtime = "2014-10-30t08:01:01-05:00" timezoneid ="pacific standard time". outputdateformat ="yyyy-mm-ddthh:mm:ss zz"
datetime finaloutputtime = timezoneinfo.converttimebysystemtimezoneid(inputtime,timezoneid);
string finalformatteddate =finaloutputtime.tostring(outputdateformat);
you can use datetimeoffset
instead of datetime
handle this:
datetimeoffset time = datetime.now; string timezoneid = "pacific standard time"; timezoneinfo info = timezoneinfo.findsystemtimezonebyid(timezoneid); datetimeoffset newtime = timezoneinfo.converttime(time, info); string timeformat = newtime.tostring("yyyy-mm-dd hh:mm:ss \"gmt\"zzz"); string oldtimeformat = time.tostring("yyyy-mm-dd hh:mm:ss \"gmt\"zzz");
Comments
Post a Comment