从泰语转换为英语时的日期时间转换问题 [英] Datetime conversion issue while converting from thai to english

查看:100
本文介绍了从泰语转换为英语时的日期时间转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以选择语言,并为该语言设置当前文化。当用户选择泰语时,我面临着一个问题。由于日历比格鲁吉亚日历提前543年,日期显示为2017 + 543。这可以。但问题是,当我试图将日期从泰国文化转换为英国文化时,不知何故它增加了543年。意味着目前它是 2017 + 543 = 2560 。它增加了543年 2560 + 543 = 3103 。有谁可以帮助我吗。请注意 CurrentThread.CurrentCulture 将是泰语。这是一个代码片段



我尝试过:



< pre lang =C#> DateTime dt = DateTime.Now;
字符串 format = MM / dd / yyyy hh:mm:ss;

System.Globalization.CultureInfo us = new System.Globalization.CultureInfo( en-US);
IFormatProvider culture = System.Globalization.CultureInfo.CreateSpecificCulture( en-US);
String strDate = dt.ToString(format);

DateTime serverSysdate = DateTime.ParseExact(strDate, MM / dd / yyyy hh:mm :ss,culture);

解决方案

错误的方法!



你不应该' t将日期转换为字符串,然后转换为日期!你必须在约会上工作!

想象一下,世界上某个时区的时间不同!



欲了解更多详情,请参阅:

使用日历 [< a href =https://msdn.microsoft.com/en-us/library/82aak18x(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]

使用日期和时间执行算术运算 [ ^ ]

如何:使用时区日期和时间算术 [ ^ ]

转换Betw een DateTime和DateTimeOffset [ ^ ]

转换时区之间的时间 [ ^ ]

如何:解决不明确的时代 [ ^ ]

如何:让用户解决不明确的时代 [ ^ ]



示例:

  //  在当地时区获取时间 
DateTime thisTime = DateTime.Now;
Console.WriteLine( {0}区域中的时间:{1},TimeZoneInfo .Local.IsDaylightSavingTime(thisTime)?
TimeZoneInfo.Local.DaylightName:TimeZoneInfo.Local.StandardName,thisTime);
Console.WriteLine( UTC时间:{0},TimeZoneInfo.ConvertTimeToUtc(thisTime ,TimeZoneInfo.Local));
// 获取GTB标准时区 - (GMT + 02:00)雅典,伊斯坦布尔,明斯克
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById( GTB标准时间);
DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime,TimeZoneInfo.Local,tst);
Console.WriteLine( {0}区域中的时间:{1},tst .IsDaylightSavingTime(tstTime)?
tst.DaylightName:tst.StandardName,tstTime);
Console.WriteLine( UTC时间:{0},TimeZoneInfo.ConvertTimeToUtc(tstTime ,tst));


I have an application where users can select language and current culture will be set for that language. I an facing issues when users select thai language. Since thai calendar is 543 years ahead from georgian calendar, date shows as 2017+543. This is fine. But the problem is when I try to convert date from thai culture to english culture, somehow it adds more 543 years. Means currently it's 2017+543 = 2560. It adds more 543 years 2560 + 543= 3103. Can someone help me please. Please note that CurrentThread.CurrentCulture will be thai. Here is a code snippet

What I have tried:

DateTime dt = DateTime.Now;
String format = "MM/dd/yyyy hh:mm:ss";

System.Globalization.CultureInfo us = new System.Globalization.CultureInfo("en-US");
IFormatProvider culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
String strDate = dt.ToString(format);

DateTime serverSysdate = DateTime.ParseExact(strDate, "MM/dd/yyyy hh:mm:ss", culture);

解决方案

Wrong approach!

You shouldn't convert date into string and then into date! You have to work on dates!
Imagine, a time in a world differs between time zones!

For further details, please see:
Working with Calendars[^]
Performing Arithmetic Operations with Dates and Times[^]
How to: Use Time Zones in Date and Time Arithmetic[^]
Converting Between DateTime and DateTimeOffset[^]
Converting Times Between Time Zones[^]
How to: Resolve Ambiguous Times[^]
How to: Let Users Resolve Ambiguous Times[^]

Example:

// Get time in local time zone 
 DateTime thisTime = DateTime.Now;
 Console.WriteLine("Time in {0} zone: {1}", TimeZoneInfo.Local.IsDaylightSavingTime(thisTime) ?
                   TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName, thisTime);
 Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local));
 // Get GTB Standard Time zone - (GMT+02:00) Athens, Istanbul, Minsk
 TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("GTB Standard Time");
 DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst);      
 Console.WriteLine("Time in {0} zone: {1}", tst.IsDaylightSavingTime(tstTime) ?
                   tst.DaylightName : tst.StandardName, tstTime);
 Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));


这篇关于从泰语转换为英语时的日期时间转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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