-1

Hi all I have some doubt for datetime .

For the system.DateTime.Now is return current local date .

The below code is helps to get current indian datetime

Dim IndianTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")
Dim IndianDateTime = TimeZoneInfo.ConvertTime(USDateTime, IndianTimeZoneInfo)

But at this time The US and UK , etc country have different datetimes . How can i manage it in simple code ?

The main doubt for the above code is got indian time only , But if we want to US or other country time , We need to separate codes for different countries (if yes, Then we write lot of codes , It's good ?)? or any other idea's there ?

Please advice me . Thanks !

Edit:

simple my question is how to store current date time for different different countries?

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • You have to perform as many declarations/storages/calculations as different values you want. This is true for everything. What do you expect? just one variable delivering hundreds of different results? Or please, elaborate more on your answer if I haven't understood your point properly. – varocarbas Sep 29 '13 at 08:47
  • 4
    The easiest thing to do is to get the time in UTC format using `DateTime.UtcNow`. This gets the local time expressed as the Universal Time Coordinate. Use this time in your storage, and convert it at retrieval into the local time. – pixelbadger Sep 29 '13 at 08:54
  • @magos I don't see any advantage between what you propose and what the OP is already doing. Also this does not answer the OP's concern: he is not worried about calculating just one time, but various ones and how can reduce the number of calculations. What you propose, equivalently to what the OP did and to any other approach involves as many calculations/storages as different time zones you want to account for. – varocarbas Sep 29 '13 at 09:04
  • 2
    It's difficult to understand what OP is after, but I'd presumed he was wanting to store the client's local time in multiple time zones, and then display a conversion of that local time in other time zones. So rather than storing the DateTime *and* the originating time zone, then performing conversion on this, you'd just store the UTC time. – pixelbadger Sep 29 '13 at 09:08
  • @magos as far as the reference point is kept constant, it does not really matter its value (a scale starting in zero or in -100 is equally efficient). On the other hand, if the OP is interested in a specific value, perhaps it would be better to set the reference on this value. What the OP is apparently looking for is reducing what is "unreduceable". (PS: if you don't type '@'my-nick I don't know that you have written something as far as this is neither your question nor your answer). – varocarbas Sep 29 '13 at 09:19
  • My question is how to store current date time for different different countries ? did you understood ? – Ramesh Rajendran Sep 29 '13 at 10:36
  • 4
    @RameshRajendran, rather than saving different time for different countries you should save UTC time and at the time of display you can change it according to the country. – Kundan Singh Chouhan Sep 29 '13 at 10:41
  • @RameshRajendran Have you seen: http://stackoverflow.com/questions/14251553/c-sharp-and-current-local-time-for-different-countries – Rezoan Sep 29 '13 at 10:46
  • @KundanSinghChouhan you are saying that you can rely on UTC and that this solves the OP's concern of storing multiple variables? Can you please explain me what is the difference between UTC and India-Time? Spain Time according to UTC is +2 and according to India (New Delhi) is -4:30, what is the exact difference between these two approaches and why is one better than the other? Also how can you deal with Spanish and Indian time without declaring two variables/storages/calculations? – varocarbas Sep 29 '13 at 11:44
  • As explain from the start, there is no magic solution allowing you to store many things in a lower number of variables: you have to declare as many variable and set up as many time-conversion system as time-zones you want to account for. The calculations are really simple (+- certain hours) independently upon the system you choose: the original one in your question or the UTC one which, according to all the other commentators, seems to have marvellous features which I cannot see. – varocarbas Sep 29 '13 at 11:47
  • Hello sorry for the late ! internet problem . – Ramesh Rajendran Sep 29 '13 at 11:54
  • @Rezoan . Can you please explain me what is the difference between UTC and India-Time? Please answer this question . I think UTC time is different then indian time – Ramesh Rajendran Sep 29 '13 at 11:55
  • And The system.DateTime.Now get server date time , I don't know how to solve this anyone tell me the answer with sample code ! – Ramesh Rajendran Sep 29 '13 at 11:57
  • 1
    You are over-complicating things unnecessarily. This is a very easy problem with a very simple solution. You have to create as many variables as time zones you want to defined (there is no way to avoid this). Each time zone has to be define with respect to the same reference time; usually, people rely on UCT/GMT which is reference time used everywhere; but there is no problem to implement a different reference in your application. You have to hardcode (or rely on libraries containing these values) the definitions for each time zone on account of the reference one, for example... – varocarbas Sep 29 '13 at 11:59
  • @varocarbas I want to 100 country date time , So we create 100 variables ? and how to check current country name ? – Ramesh Rajendran Sep 29 '13 at 12:03
  • ... if you want to set India time as reference and want to store Spain, Japan, Peru... you can get the times easily, for example, via google: current time in Spain 14:00, current time in India 17:30. Spain time = India time (ref) - 3:30; current time in Japan: 21:00 Japan time = India time (ref) + 3:30, etc. You have to do this for all the countries independently upon the reference time you account for. Also you have to bear in mind that the time in countries change (Daylight saving time) in different moments and you would have to bring this variable into account for each country too. – varocarbas Sep 29 '13 at 12:04
  • 1
    There are libraries or .NET variables helping you with the work (or you can do it manually by just searching for the given country time as suggested), but you will have to declare 100 variables (one per country) no matter what. These variables will not only store the given +/- hour lag but other properties (like when the daylight saving time kick in, for example). You can do this via class or via arrays, but you will have to account for 100 elements in any case (even, in case of relying on a library, you might retrieve the information dynamically but there will be 100 "different treatments"). – varocarbas Sep 29 '13 at 12:06
  • @RameshRajendran So you don't know the difference? you need to consider a base/standard time that can help you out to find the other time of different zone? you can better search google for UTC. e.g: https://www.google.com.bd/search?q=what+is+the+difference+between+UTC+and+indian+time&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&gws_rd=cr&ei=fRdIUpv8ComMrQed8IDADg#q=what%20is%20UTC%20time&rls=org.mozilla:en-US%3Aofficial see this link you will get the difference between the current UTC time and if you lived in india see your computer time. – Rezoan Sep 29 '13 at 12:08
  • @varocarbas . Oh! SO i think it's complicated and need to long time ! – Ramesh Rajendran Sep 29 '13 at 12:18
  • @RameshRajendran no, it is pretty easy. Just have to do it. – varocarbas Sep 29 '13 at 12:29
  • Yes i know thats easy . But we need to write lot of code for all countries ! can you give any sample code or link ? – Ramesh Rajendran Sep 29 '13 at 12:46

2 Answers2

2

@RameshRajendran here is an article and some code from the article that I'm sure will not complete your project. But hopefully it with help you see it from a different perspective. As @varocarbas pointed out it will serve you well to create a table to store your final compiled info.

Link to article: http://msdn.microsoft.com/en-us/library/vstudio/bb397781(v=vs.110).aspx

You can use the SupportsDaylightSavingTime property to see if there are rules for the timezone.

You can also use the Object Browser in Visual Studio to see the members of the System.TimeZoneInfo.

I tested this with the following in the index ActionResult of the controller and the html helper in the index view. Again, This is just to help you see your way past the 'Lots of work' block you seem to be uncomfortable with.

I've written this code using c#, but I'm sure you'll have no trouble creating the equivilant in VB.

[ Controller ]

// Sample code from the article.
// Note: the collection only contains full hour offsets (i.e. New Delhi is in a half hour zone)
ReadOnlyCollection<TimeZoneInfo> tzCollection;
tzCollection = TimeZoneInfo.GetSystemTimeZones();

foreach (TimeZoneInfo timeZone in tzCollection)
    {
        Console.WriteLine("   {0}: {1}", timeZone.Id, timeZone.DisplayName);
    }

// My modified version of its use.
// this code will create a dropdownlist with all the timezones available on my machine.
var theTimesInfo = new List<TimeZoneInfo>(tzCollection);
var theZones = new SelectList(theTimesInfo, "Id", "DisplayName", 1);

ViewData["theZones"] = theZones;

string myLocalZone = "Eastern Standard Time";
TimeZoneInfo myLocalDateTime = TimeZoneInfo.FindSystemTimeZoneById(myLocalZone);

string theZoneSelected = Request.QueryString.Get("theZones");
if (theZoneSelected == string.Empty || theZoneSelected == null)
{
    theZoneSelected = myLocalZone;
}

TimeZoneInfo selectedTimeZone = TimeZoneInfo.FindSystemTimeZoneById(theZoneSelected);
DateTime CurrentTimeZoneDateAndTime = TimeZoneInfo.ConvertTime((DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified)), myLocalDateTime, selectedTimeZone);

ViewBag.CurrentTimeZoneSelected = selectedTimeZone.DisplayName.ToString();
ViewBag.CurrentDateTimeSelected = CurrentTimeZoneDateAndTime.ToString();

[updated] edited the view code to show the values returned.

[ View ]

<p>
    The Select TimeZone is: @ViewBag.CurrentTimeZoneSelected<br />
    The current Date and Time for the Selected TimeZone is: @ViewBag.CurrentDateTimeSelected
</p>

<form>
    @Html.DropDownList("theZones", ViewData["theZones"] as SelectList)
    <input type="submit" name="submit" value="Select This Time Zone" />
</form>

You can use the foreach loop to iterate through the collection and add it to your db table. Then if you need to, you can add individual entries to the table, or adjust the +- as needed.

nocturns2
  • 663
  • 10
  • 17
  • "Eastern Standard Time" is okay . But i want to any lot other zone , how can do this ? – Ramesh Rajendran Sep 29 '13 at 15:33
  • It's helps to get only one timezone for dropdown selected value . But i want to store all countries datetime on run time . is it possible ? – Ramesh Rajendran Sep 29 '13 at 15:35
  • @Ramesh Rajendran, the sample code is intended to illustrate what is possible with a small amount of code. The dropdown was intended to give you a visual of the implementation. The GetSystemTimeZones can help you generate a collection of timezones available on your system. The collection will include all the full hour timezones. In a case where the country is located in a fraction hour timezone, like for instance New Delhi is in the UTC +5:30 (half hour timezone). – nocturns2 Sep 29 '13 at 16:08
  • You will need to refer to a resource like date and time sites. A link to one is http://www.timeanddate.com/worldclock/city.html?n=176. If you first create a table of timezone information using a variation of the sample code you can add additional country timezone info to that table and eventually you'll have a resource you can turn to. Ultimately, you'll need to create a list (i.e., table) or you might search for a pre-build one. – nocturns2 Sep 29 '13 at 16:09
  • +1 for good explanation + effect . But i have another one doubt , for the GetSystemTimeZones had collection of timezones , but how can find current country timezone ? Sorry i am little confusion . – Ramesh Rajendran Sep 29 '13 at 16:30
  • Converting from the collection that GetSystemTimeZones is in to the SelectList worked for me very well. I actually did the conversion in the View with one line of code though: "var list = new SelectList(new List(TimeZoneInfo.GetSystemTimeZones()), "Id", "DisplayName", 1);" maybe that will help someone later on. +1 – AVenger Apr 29 '14 at 13:43
1

You can save the time as the UTC time, using datetime.UtcNow.

You can convert to another country's time zone using TimeZoneInfo.ConvertTime, as you did in your example, converting either Now or UtcNow dates.

The problem seems to be how to get all the timezone info. You can do that with GetSystemTimeZones. The official timezone name is in timeZoneInfo.id.

Dim zones As System.Collections.ObjectModel.ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones()
Dim hSpan As TimeSpan

For Each zone As TimeZoneInfo In zones
  If InStr(LCase(zone.Id), "greenland") > 0 Then
    ' zone has the timezoneinfo for greenland.
    ' the following line assigns hSpan the difference between Greenland time and UTC.
    ' Be careful -- there may be more than one Greenland time. A combo box would be safer.
    hSpan = zone.GetUtcOffset(Now)
    exit for
  End If
Next zone

You can get the current time zone offset of the client browser in Javascript, something like this:

function GetClientUTC()
{
var now = new Date();
var offset = now.getTimezoneOffset();
}
xpda
  • 15,585
  • 8
  • 51
  • 82
  • Now your code helps to get greanland time, but i need to all other countries datetime , So i think we add more code in the file ? – Ramesh Rajendran Sep 29 '13 at 15:34
  • We need to use this code "TimeZoneInfo.ConvertTime" many time for convert all countries ? if i have 100 countries , I need convert 100 time (write the convert code 100 time?) ? – Ramesh Rajendran Sep 29 '13 at 16:38
  • How will you identify the countries for which you need the time zone info? You can use that instead of the greenland constant. Ideally, you can get the local time zone info from the user and use the complete name. This was only an example. You should only convert the time when you need it, as you need it. That way it will continue to work when time zones change, and you won't have to store it all in your application. – xpda Sep 29 '13 at 16:44
  • okay ! my application have not login and user select country option , just like for face book like option (not proper), So at this time how can get current browser running country ? and how can we store the like button clicked time which current country datetime ? – Ramesh Rajendran Sep 29 '13 at 16:50
  • +1 for effect . But i have more and more some doubts . sorry for the disturbing ! – Ramesh Rajendran Sep 29 '13 at 16:52
  • I added getting the browser timezone offset to my answer – xpda Sep 29 '13 at 20:57