Click here to Skip to main content
12,997,531 members (58,477 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 7:30am
Updated 10-Mar-12 7:46am
Rate this: bad
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:[^].

[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:[^],[^].

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:[^].

For format specifiers, please see:[^],[^].

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

Shahin Khorshidnia 11-Mar-12 5:34am
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 11-Mar-12 13:42pm
It is vague, I expressed it in my answer.
Thank you.
SAKryukov 11-Mar-12 13:50pm

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.
Frex89 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 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.
Rate this: bad
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" );
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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
             return _date;
             _date = value;
             DateString = value.ToString("dd/mm/yyyy");
     private string _dateString;
     public string DateString
             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);            
SAKryukov 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.
Shahin Khorshidnia 11-Mar-12 17:29pm
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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 11 Mar 2012
Copyright © CodeProject, 1999-2017
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