-3

I've been trying to convert a date to Indian format with CultureInfo as hi-IN. I am trying the below code, but I am not getting successful result. What am I missing?

//item.Value=2/27/1998 3:56:98

DateTime.ParseExact(item.Value, "dd/mm/yyyy", System.Globalization.CultureInfo.GetCultureInfo("hi-IN"))

I even tried this:

TimeZoneInfo timeZoneInfo;
DateTime dateTime;
//Set the time zone information to US Mountain Standard Time 
timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
//Get date and time in US Mountain Standard Time 
dateTime = 
TimeZoneInfo.ConvertTime(Convert.ToDateTime(item.Value), timeZoneInfo);

Is there any way I can get the date format in "dd/mm/yyyy"?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Running Rabbit
  • 2,634
  • 15
  • 48
  • 69
  • Please note that ``DateTime`` has no inherent format; it only acquires a particular language, culture, or country-specific format by formatting it. Thus, if your source date format is in en-US format (your comment line sure looks like it), then you need to parse it using culture en-US, and then reformat it using culture hi-IN. – dumetrulo Jul 26 '17 at 09:53
  • Just to be sure `item.Value` is a string? – Jonas Jul 26 '17 at 09:53
  • "I am not getting successful result" - what are you getting? Be precise when reporting errors! – Chris Jul 26 '17 at 09:53
  • yes its a string but in date format – Running Rabbit Jul 26 '17 at 09:55
  • 2
    Though that having been said your parse exact format is `"dd/mm/yyyy"` which sure doesn't look like it will match `2/27/1998`, let alone `2/27/1998 3:56:98` (your parse format must match the entirety of the input string - ie exact). I am also not sure what time system you are using that has 98 seconds in a minute... – Chris Jul 26 '17 at 09:55
  • 1
    as others have mentioned, the input is invalid as you have **too many seconds in your minute**. If you correct the input to `"2/27/1998 3:56:58"`, you can just do the following `var date= DateTime.Parse(item.value).ToString("dd/MM/yyyy");` – Dennis.Verweij Jul 26 '17 at 10:08
  • ParseExact need same format and you are using "dd/mm/yyyy" , which is not even close to 2/27/1998 3:56:98 – Saurabh Jul 26 '17 at 10:08

2 Answers2

1

Please use the following code....

 var time = DateTime.Parse(DateTime.Now.ToString());
        var clientZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
        var utcTime = TimeZoneInfo.ConvertTimeToUtc(time, clientZone).ToString("dd/MM/yyyy");
Koderzzzz
  • 849
  • 8
  • 18
1

Try this

TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
CultureInfo cultureinfo = new System.Globalization.CultureInfo("en-US");
DateTime dtUS = DateTime.ParseExact("2/27/1998 3:56:00", "M/dd/yyyy h:mm:ss", cultureinfo);
DateTime dtIndia = TimeZoneInfo.ConvertTime(dtUS, timeZoneInfo);
Ben
  • 763
  • 1
  • 5
  • 14