- Home /
C# get-time code
I want to get my computer time in c# script and then use the time for the if cmmand like tihs: if(time == 14.00){} but i can't figure out how can i do it
Answer by Landern · Oct 23, 2014 at 01:32 PM
DateTime now = DateTime.Now; // could do DateTime.Now.ToLocalTime(); // but.... but you shouldn't need a conversion.
if (now.Hour == 14)
{
// do things
}
DateTime.Now is already in local time ;) You need that method when you have a DateTime value that isn't. For example DateTime.UtcNow is in UTC
@Bunny83, DateTime kind is unspecified by default. Generally speaking DateTime.Now is the computers current time, this doesn't mean its the local time.
An example, we(real job in the adult world(sadness)) host many web applications for companies all over the united states, we honor their time zone for simplicity on the systems, but the time on the system doesn't actually refer to the location that computer resides(be it the SAN or rack).
We know Date and Time is a difficult subject that we can go on and on about. Joda time for java was meant to solve this problem, Jon Skeet(and friends) converted it to c# and called Noda time but these solutions/libraries don't answer/solve all the issues as is discussed to death on StackOverflow.
ToLocalTime requires $$anonymous$$ind.Local, that wouldn't apply in this case but as i stated in the comment comment, a conversion isn't required and my original answer is DateTime.Now, i mentioned it to leave a bread crumb trail if @-Grocfex0000 wanted to follow.
If you use DateTime.UtcNow a conversion takes place to, the object is $$anonymous$$ind Utc.
In the end our answer was the same, guess i'm a little confused what you're driving at.
@Landern: Uhm i'm not sure where you got your information from, but the $$anonymous$$S documentation states that DateTime.Now returns the time as local time. Also if i do
Debug.Log(System.DateTime.Now.$$anonymous$$ind);
i get "Local". Of course it's the computer's time according to the computers localization. But that's normal and expected since "local" refers to the localization of the computer, nothing else.
If you host a server in a certain timezone but it's running with a different timezone setting, only the timezone configured in the localization of the server counts. It doesn't matter where the server is located physically. It could be on the moon (If you have an internet connection there :P).
The two conversion methods ToLocalTime and ToUniversalTime can only convert between local time and UTC. That makes them actually pretty useless.
For example if you call ToLocalTime on a time that is already "Local", nothing happens. If you call it on a time of kind UTC or unspecified it will do a conversion from UTC to local time. If your time is unspecified and not UTC you will get the wrong value.
TimeZoneInfo is what you need to convert freely between arbitrary timezones. However when it comes to servers you usually use UTC anyways, no matter where it's located. The server time can be converted on the client for displaying if necessary. That's what most websites do, including UA.
@Bunny83, you're correct, i flipped between tabs and mistakenly stated the default enumeration for $$anonymous$$ind.
In regards to the server, technically the dates i refer to are internally stored as in a ISO8601 format since the format allows sorting by string convention and includes the current offset as it relates to daylight savings at the time of ingestion by the host system. We know that daylight savings settings are changed with updates from $$anonymous$$icrosoft and begin to creep or overall lose scope with time.
I very much wish i could display UTC for our clients, but that would not be useful given the scope of my work and the data that is used by the users.
Apologies for the conflated reply, i offer no excuses.
Answer by _Grocfex0000 · Oct 23, 2014 at 08:03 PM
Thanks everybody I was able to use this: System.DateTime somVariable = System.DateTime.Now;
and then if(somVariable.Hour >= 2){
// mah stuff
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Referencing a script on the same object comes back with an error 4 Answers
Fade in AudioLister over time #c 1 Answer
Flip over an object (smooth transition) 3 Answers