How to Convert Different Timezone values into Indian Time using C#.net

Bangalore: If you are developing an application,which captures timings from your local machines, and your clients are from different time zones, then if you are using DateTime.now() function you will get the current time in that area only,But if your server is following IST,you need to convert the time zones into Indian Standard timings.
So here is the code..

            DateTime crnttime = DateTime.Now;
            TimeZoneInfo tzn = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
            DateTime IndianTme = TimeZoneInfo.ConvertTime(crnttime,tzn);
            string time = IndianTme.ToString("yyyy-MM-dd HH:mm:ss");
            MessageBox.Show("Time is " + time);

First we will get the current time from local machine and we will declare a TimeZoneInfo object, and there we will set the "India Standard Time" and convert it into IST as per the third line.
For TimeZoneInfo library you need  .Net framework 3.5 and above.
If you want time in 24 hour format you have to convert time into string as per the third line code.
          string time = IndianTme.ToShortDateString();
If you want to display only Date you can use the above code.
         string time = IndianTme.ToShortTimeString();
This code for displaying time only.

            System.TimeZone t1 = System.TimeZone.CurrentTimeZone;
            string timez = t1.StandardName;
For displaying current timezone name you can use above code.

/* androidsharp */

0 comments: