Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by _Grocfex0000 · Oct 23, 2014 at 01:30 PM · c#timeifget-time

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Oct 23, 2014 at 01:36 PM

System.DateTime.Now

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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
 }
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 23, 2014 at 01:42 PM 0
Share

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

avatar image Landern · Oct 23, 2014 at 02:24 PM 0
Share

@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.

avatar image Bunny83 · Oct 23, 2014 at 11:46 PM 0
Share

@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.

avatar image Landern · Oct 24, 2014 at 12:17 PM 0
Share

@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.

avatar image
0

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges