Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by DevMerlin · Mar 16, 2013 at 04:34 AM · timemathfdate

How can I get the time since the epoch date in Unity3D?

In normal C#, I can usually use a function like this:

 private double getTime() {
 double dateReturn =
     Math.Round((DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds);
     // note that (..date..).TotalMilliseconds returns a number such as
     // 1606465207140.45 where
     // 1606465207140 is ms and the ".45" is a fraction of a ms
 return dateReturn;
 }
 

However, most of Unity's C# uses either int or floats. This returns a double, which I am unsure how to use in Mathf functions like Mathf.min/max, which call for ints.

Comment
Add comment · Show 2
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 · Nov 27, 2020 at 02:24 PM 0
Share

That's a super-handy code sample, thanks! :)

avatar image DevMerlin Fattie · Nov 28, 2020 at 12:50 AM 0
Share

No problem! Probably plenty of better ways to do it now. Ancient question at this point. :)

3 Replies

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

Answer by sonnyyap · Jan 14, 2015 at 07:26 AM

 System.DateTime epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
 int cur_time = (int)(System.DateTime.UtcNow - epochStart).TotalSeconds;

This will give you the current time (the number of seconds that have passed since 1970/1/1)

Comment
Add comment · Show 1 · 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 adrianseeley · May 07, 2015 at 04:53 PM 0
Share

If you are trying to match JavaScript new Date().getTime(); you will need to multiply cur_time in this answer by 1000 to get milliseconds.

avatar image
2

Answer by Astrydax · Sep 12, 2016 at 04:09 PM

For me, I achieve this with a static class.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public static class Epoch  {
 
     public static int Current()
     {
         DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
         int currentEpochTime = (int)(DateTime.UtcNow - epochStart).TotalSeconds;
 
         return currentEpochTime;
     }
 
     public static int SecondsElapsed(int t1)
     {
         int difference = Current() - t1;
 
         return Mathf.Abs(difference);
     }
 
     public static int SecondsElapsed(int t1, int t2)
     {
         int difference = t1 - t2;
 
         return Mathf.Abs(difference);
     }
 
 }


It is worth noting that if you use 1-1-1970 as your epoch, your code will break on 1-19-2038. This is because the date 1-19-2038 is 2147483647 seconds after 1-1-1970. The number 2147483647 is the largest number a 32-bit signed Int can hold. To avoid this, you can change the line

 DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

to a more recent date in the past. An Int epoch spans 136 years.

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 sonnyyap · Sep 15, 2016 at 04:36 AM 0
Share

I see. A static class looks like a good idea. And yeah it is probably better to change the epoch date to a recent one. Thanks for pointing out!

avatar image Emmanuel_Charon · Oct 28, 2016 at 10:34 AM 2
Share

To avoid the 1-19-2038 problem, just use a long ins$$anonymous$$d of an int

avatar image
1

Answer by codewing · Nov 23, 2021 at 01:41 PM

 DateTimeOffset.Now.ToUnixTimeMilliseconds()

This is enought

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

15 People are following this question.

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

Related Questions

Jump with mathf.lerp problem 2 Answers

Using real-world date and time for in-game time mechanics 1 Answer

How to make a date/time system with a fast forward feature? 2 Answers

SmoothDamp keeps changing back 0 Answers

How do I increase a speed variable over time? 1 Answer


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