转换所需的时间帮助 [英] Convert time help needed

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

问题描述

大家好!有人可以帮我吗?我有一个来自DateTime的时间.现在,我想将其转换为意大利时间!我应该怎么办?? tx

Hi guys! could someone help me? I have a time from DateTime.Now and I want to convert it to italian time! what should i do?? tx

推荐答案

没有意大利时间"之类的东西.时间始终为System.DateTime.如果您需要用意大利语呈现它,那不是时间,这是字符串,与时间无关.

但是,如果您指的是意大利时区,则需要使用时区类System.TimeZoneInfo,请参见:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx [ ^ ].

我对安德烈·克拉克(AndréKraak)的回答给予了正确的解释[END EDIT]

如果您需要一个字符串,请继续阅读:

您需要使用System.DateTime.ToString(IFormatProvider)ToString(String, IFormatProvider)来考虑意大利文化.您需要使用ICultireInfo的实例,因为它实现了IFormatProvider.请在此处查看代码示例:
http://msdn.microsoft.com/en-us/library/ht77y576.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx [ ^ ].

当然,请使用适当的区域性(it-IT)代码代替"ja-JP".



在前两段中添加了另一种方法和意大利文化代码,以使其更加清晰.

您可以在许多文档中找到文化代码列表,例如,在这里:
http://sharpertutorials.com/list-of-culture-codes/ [ http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].

格式说明符可以与IFormatProvider一起使用,也可以不带IFormatProvider一起使用.和IFormatProvider可以与或不与格式说明符一起使用.

假设您需要一个意大利语的日期时间字符串:

There is no such thing as "Italian time". Time is always System.DateTime. If you need to present it in Italian, it''s not time, this is string, which has nothing to do with time.

However, if you mean Italian time zone, you need to use the time zone class System.TimeZoneInfo, please see:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx[^].

I credit the answer by André Kraak who correctly explained it [END EDIT]

If you need a string, keep reading:

You need to use System.DateTime.ToString(IFormatProvider) or ToString(String, IFormatProvider) to take into account Italian culture. You need to use the instance of ICultireInfo as it implements IFormatProvider. Please see the code sample here:
http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^],
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx[^].

Of course, put appropriate culture ("it-IT") code instead of "ja-JP".



Added another method and Italian culture code in the previous two paragraphs, to make it more clear.

You can find a list of culture codes in many documents, for example, here:
http://sharpertutorials.com/list-of-culture-codes/[^].

For format specifiers, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

A format specifier can be used with IFormatProvider or without it; and IFormatProvider can be used with or without format specifier.

Suppose you need a date-time string in Italian:

System.DateTime time = System.DateTime.Now; 
System.IFormatProvider culture = new System.Globalization.CultureInfo("it-IT"); //Italy
// all in Italian language:
string defaultTimeString = time.ToString(culture);
string fullDateTimePatternShortTime = time.ToString("f", culture);
string fullDateTimePatternLongTime = time.ToString("F", culture);
string fullDayMonthYearOnly = time.ToString("dd MMMM yyyy", culture); //full month name, 4-digit year
//or any other combinations



-SA



—SA


使用 TimeZoneInfo类 [ ^ ]及其转换功能之一.

例如:
Use the TimeZoneInfo Class[^] and one of its conversion functions.

For example:
TimeZoneInfo::ConvertTimeBySystemTimeZoneId( DateTime.Now, "W. Europe Standard Time" );


你好


Hello


public class ItalianDate
 {
     private string[] months = new string[12] { "gennaio", "febbraio", "marzo", "aprile", "aprile", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre" };
     private string[] dayOfWeeks = new string[7] { "domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato" };


     private DateTime _date;
     public DateTime Date
     {
         get
         {
             return _date;
         }
         set
         {
             _date = value;
             DateString = value.ToString("dd/mm/yyyy");
         }
     }

     private string _dateString;
     public string DateString
     {
         get
         {
             return _dateString;
         }
         private set
         {
             _dateString = value;
         }
     }

     public ItalianDate(DateTime date)
     {
         Date = date;
     }

     public string GetMonth()
     {
         return months[Date.Month - 1];
     }

     public string GetDayOfWeek()
     {
         return dayOfWeeks[(int)Date.DayOfWeek];
     }
 }




然后您可以使用该类:




Then you can use the class:

ItalianDate italian = new ItalianDate(DateTime.Now);            
Console.WriteLine(italian.GetDayOfWeek());
Console.WriteLine(italian.GetMonth());
Console.WriteLine(italian.DateString);


这篇关于转换所需的时间帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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