1

I need to convert to specific time zone from UTC time using UTC time Offset.

I tried as following:

 DateTime utcDateTime = DateTime.UtcNow;
 TimeSpan offSet = TimeSpan.Parse(timeoffset.ToString());
 DateTime newDateTime = utcDateTime.Add(offSet);

But it doesn't adds up the offset with UTC. Is there any other way?

2 Answers 2

1
 DateTime utcDateTime = DateTime.UtcNow;
 TimeSpan offSet = TimeSpan.Parse(timeoffset.ToString());
 DateTime newDateTime = utcDateTime.Add(offSet);

instead of using TimeSpan.Parse(timeoffset.ToString()); i used TimeSpan.FromHours((double)offset);

  DateTime utcDateTime = DateTime.UtcNow;
     TimeSpan offSet = TimeSpan.FromHours((double)offset);
     DateTime newDateTime = utcDateTime.Add(offSet);

It worked perfectly fine for me.

Thank You

Sign up to request clarification or add additional context in comments.

Comments

0

You can do in a simple way, here change time zone as per your requirement.

public static DateTime GetISTDate()
{
     DateTime utcDateTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now, TimeZoneInfo.Local);
     var ISTtime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
     return ISTtime;
} 

3 Comments

i Cant use TimeZoneInfo.FindSystemTimeZoneById since i dont know the timezone id. AM storing UTCOffset time in DB and reading from there.
@ShivaPrasad: It seems like you are trying to get datetime on basic of off set.
@ShivaPrasad: Then you need to do in this way: utcDateTime + offSet;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.