Reputation: 85
I want to add time to the current time or in other words,i have time in UTC + 0, i want that time in UTC + 5.00, I can subtract as many hours as i can
Code:
using System;
namespace week3
{
class LocalTime
{
static string Time, City;
static decimal time;
public LocalTime(string city, double add) {
Time = Convert.ToString(DateTime.Now + TimeSpan.FromHours(add));
Time = Time.Substring(11, 5);
time = Convert.ToDecimal(Time.Substring(0, 2) + "." + Time.Substring(3, 2));
City = time.ToString();
Time = City.Substring(0, 2) + ":" + City.Substring(3, 2);
City = city;
DisplayTimeAndCity("", "");
}
static void DisplayTimeAndCity(string x, string y)
{
Console.WriteLine(City + " - " +Time);
}
}
class London : LocalTime {
public London() : base("London", 0) {
}
}
class NewYork : LocalTime
{
public NewYork(): base("NewYork", 5)
{
}
}
class Tokyo : LocalTime
{
public Tokyo(): base("Tokyo", -9)
{
}
}
class HongKong : LocalTime
{
public HongKong(): base("Hong Kong", -8)
{
}
}
class Test {
static void Main() {
London a = new London();
NewYork b = new NewYork();
}
}
}
Upvotes: 0
Views: 1722
Reputation: 3118
As you are trying to represent the time in different time zones you might find TimeZoneInfo.ConvertTimeBySystemTimeZoneId to be useful. Rather than defining several classes you might prefer to use something from the framework. The documentation for the strings which identify each zone is in the TimeZoneInfo.FindSystemTimeZoneById Method and the strings relate to the TimeZone.Id property. I tested this on Windows 8.1, it worked fine.
The time zone identifier is a key string that uniquely identifies a particular time zone. In Windows XP and Windows Vista, it corresponds to the subkeys of the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zone branch of the registry. It can be passed as a parameter to the FindSystemTimeZoneById method to retrieve a particular time zone from the registry.
void Main()
{
DateTime currentTime = DateTime.Now;
Console.WriteLine("Current Times:");
Console.WriteLine();
Console.WriteLine("Los Angeles: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time"));
Console.WriteLine("Chicago: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time"));
Console.WriteLine("New York: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time"));
Console.WriteLine("London: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time"));
Console.WriteLine("Moscow: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time"));
Console.WriteLine("New Delhi: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time"));
Console.WriteLine("Beijing: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time"));
Console.WriteLine("Tokyo: {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time"));
}
This lists the time zones that can be found on the local machine:
ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
Console.WriteLine("The local system has the following {0} time zones", zones.Count);
foreach (TimeZoneInfo zone in zones)
Console.WriteLine(zone.Id);
Upvotes: 1
Reputation: 100630
I think what you are looking for is "how to format DateTime
value as hours:minutes
":
var time = (DateTime.Now + TimeSpan.FromHours(3)).ToString("hh:mm");
More information on formatting options - Custom Date and Time Format Strings.
Note that other answers suggest much better ways to deal with timezones via TimeZoneInfo
or DateTimeOffset
.
Upvotes: 0
Reputation: 16796
You could also use DateTimeOffset
for your purpose:
DateTimeOffset dateTimeWithOffset = new DateTimeOffset(DateTime.UtcNow).ToOffset(TimeSpan.FromHours(5));
With that, you will keep both the time, and the offset information in a standard way.
Upvotes: 0
Reputation: 2714
To Add 5 hours to the a DateTime
you could simply use the following:
DateTime.Now.AddHours(5);
And there is also an easy function to substract from a DateTime:
DateTime.Now.Subtract(TimeSpan.FromHours(1));
Upvotes: 1