• 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
1
Question by DaveA · Jan 24, 2011 at 10:45 PM · datetimebce0077

How to get 'real' time?

I need to get the current time in good ole 'seconds since 1970' format. Using JScript.

I tried the System call DateTime.Now and DateTime.UtcNow, but I get this error:

BCE0077: It is not possible to invoke an expression of type 'Date'.

Any ideas?

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

4 Replies

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

Answer by DaveA · Jun 02, 2011 at 10:55 PM

Actually I needed 'unix' time in UTC. This is what I ended up with:

 var epochStart = new System.DateTime(1970, 1, 1, 8, 0, 0, System.DateTimeKind.Utc);
 var timestamp = (System.DateTime.UtcNow - epochStart).TotalSeconds;
Comment
Add comment · Show 2 · 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 Fattie · Apr 04, 2013 at 12:43 PM 1
Share

a priceless code fragment for lazy coders, thanks :-)

for any beginners reading - it could be useful to make timestamp an int (simply var timestamp:int = .. as above) as you may likely need it as just an int rather than it will default to a float there.

avatar image Lohoris2 · Jan 11, 2015 at 10:45 PM 1
Share

var :( :(

avatar image
8

Answer by bderooms · Aug 06, 2013 at 12:50 PM

The code fragment presented here did not give me the correct time compared to for example the Java unix time. I do get the right time however with the example in this post:

http://stackoverflow.com/questions/7983441/unix-time-conversions-in-c-sharp

The main difference is that the 8 is zero, I assume that the 8 a typo?

  var epochStart = new System.DateTime(1970, 1, 1, ->0<-, 0, 0,System.DateTimeKind.Utc);
  var timestamp = (System.DateTime.UtcNow - epochStart).TotalSeconds;
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 Bampf · May 31, 2011 at 10:06 PM

They are implemented as properties, not methods. Did you by chance include parenthesis in your call?

 var dt = DateTime.Now();  // wrong
 var dt = 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 Bravini · Jun 01, 2011 at 04:58 PM

just to provide a complementary answer, on js would be like:

var dt = Date();

var day = dt.Now.Day.ToString();

var month = dt.Now.Month.ToString();

var year = dt.Now.Year.ToString();

var hours = dt.Now.Hour.ToString();

var minutes = dt.Now.Minute.ToString();

if (parseInt(minutes) < 10) minutes = "0" + minutes;

var seconds = dt.Now.Second.ToString();

if(parseInt(seconds) < 10) seconds = "0" + seconds;

Like Bampf said, you don't access them as methods, only as properties on unityscript. Also calling new Date() gives you a zeroed date, so you need to use .Now to get the current time.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

another place to get date time from 2 Answers

Countdown timer into text c# 1 Answer

Im haveing trouble with my baisc movment script 3 Answers

calendar gui for date selection 4 Answers

PlayerPrefs not saving mission info at all. 0 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