Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# time
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
Posted 10-Mar-12 8:30am
Frex89200
Edited 10-Mar-12 8:46am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^].
 
[EDIT] 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".
 
[EDIT]
 
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
  Permalink  
v5
Comments
Shahin Khorshidnia at 11-Mar-12 5:34am
   
+5
Of course a perfect answer, as always.
But you know, the question is a little vague.
"italian time"?! I thought Frex89 wanted to translate english dateTime to italian (not to change the current timeZone to Rome)
You see, I used System.DateTime.ToString(IFormatProvider) in my solution too.
Maybe you want to revote it (If you downvoted my solution)!
SAKryukov at 11-Mar-12 13:42pm
   
It is vague, I expressed it in my answer.
Thank you.
--SA
SAKryukov at 11-Mar-12 13:50pm
   
Shahin,
 
Sorry, you did not use it. It looks like some misunderstanding.
You did not use it, and this is the problem if your answer, unfortunately.
 
Please look at the use of this method. It can be found in the article I referenced.
 
CultureInfo culture = new CultureInfo("it-IT");
string value = time.ToString(culture);
 
I'll add more detail in the answer. Thank you for indicating that it was not 100% clear.
 
I mostly added this material for you to read, as you need this understanding yourself.
 
Thank you for understanding.
--SA
Frex89 at 11-Mar-12 8:28am
   
What I was trying to do is convert time from a country(local time of the application user) to get what time and date is in Italy coz this application is used to control a server that is located in Italy..
TimeZoneInfo.ConvertTimeBySystemTimeZoneId( DateTime.Now, "W. Europe Standard Time" ) is a good way to do that!
SAKryukov at 11-Mar-12 14:15pm
   
Yes, sure. You see, the problem was that your question was vague. Thank you for clarification of it, even though it's a bit late. As a result I gave you the answers bases on two different interpretations of your question. Maybe, this is useful anyway, because you may need understanding of both aspects. (I also added an update, after [EDIT], to clarify it of Shahin).
So, will you consider to accept the answer formally (green button) -- thanks. You can accept more than one.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the TimeZoneInfo Class[^] and one of its conversion functions.
 
For example:
TimeZoneInfo::ConvertTimeBySystemTimeZoneId( DateTime.Now, "W. Europe Standard Time" );
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
Comments
SAKryukov at 11-Mar-12 14:09pm
   
Shanin, please take a look on my answer, updated.
The update should explain why you code is excessive, because format + cultureInfo do it all.
More importantly, the code using format specifier is well supportable, and yours is not.
In this way, unfortunately, your could would have only the negative impact and should not be used. It would illustrate the danger of attempts to repeat some solutions without finding a ready-to-use solution in available API, which is much better because it uses standards (like culture codes, etc.)
 
Thank you for understanding.
--SA
Shahin Khorshidnia at 11-Mar-12 17:29pm
   
Ok SA
I read your answer again (updated). You're right.
I didn't pay attention that Italian calander and English were both christian era and they could translate to each other easily with Culture.
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 105
4 PIEBALDconsult 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 11 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100