Get timestamp, date, time, milliseconds and seconds in Unity to convert each other and customize time in format

Get timestamp, date, time, milliseconds and seconds in Unity to convert each other and customize time in format

  • introduce
    • what is timestamp
    • When to use timestamps
  • Get Time
    • Get current time
    • Get timestamp
    • Date to timestamp
    • Convert timestamp to date
    • Convert timestamp to how long ago it was
    • Week
    • Custom format time
  • Summarize

Introduction

Here comes a timestamp and time conversion URL

What is a timestamp

The timestamp is the total number of milliseconds from 0:00:00 on January 1, 1970 to the present. Why is it 1970/1/1/00:00:00? Because the first computer was invented at this time, so Timestamps were born.

When to use timestamps

For example, if you want to do some time-related functions, you will basically use timestamps. Moreover, the timestamp is accurate. For example, when it comes to timing, treasure chest countdown, account ban, account ban and other related issues, the data you request from the server usually uses the timestamp to obtain the specific time. And if you communicate with the server, you can also use the timestamp to make a request response time.

Get time

Get the current time

//Beijing time
DateTime date1 = DateTime.Now;
Debug.LogError("Beijing time: " + date1);

//International time
DateTime date2 = DateTime.UtcNow;
Debug.LogError("International time: " + date2);

Get timestamp

//Time stamp method one
long now1 = DateTime.UtcNow.Ticks;
Debug.LogError("Current timestamp: " + now1);

//Time stamp method two
long now2 = DateTime.Now.ToUniversalTime().Ticks;
Debug.LogError("Current timestamp: " + now2);

Date to timestamp

//Method 1
TimeSpan st1 = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
Debug.LogError("Convert date to millisecond timestamp:" + Convert.ToInt64(st1.TotalMilliseconds));
Debug.LogError("Convert date to seconds timestamp:" + Convert.ToInt64(st1.TotalSeconds));
Debug.LogError("Convert date to minute timestamp:" + Convert.ToInt64(st1.TotalMinutes));
Debug.LogError("Convert date to hour timestamp:" + Convert.ToInt64(st1.TotalHours));
Debug.LogError("Convert date to day timestamp:" + Convert.ToInt64(st1.TotalDays));
//Method Two
TimeSpan st2 = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
Debug.LogError("Convert date to millisecond timestamp:" + Convert.ToInt64(st2.TotalMilliseconds));
Debug.LogError("Convert date to seconds timestamp:" + Convert.ToInt64(st2.TotalSeconds));
Debug.LogError("Convert date to minute timestamp:" + Convert.ToInt64(st2.TotalMinutes));
Debug.LogError("Convert date to hour timestamp:" + Convert.ToInt64(st2.TotalHours));
Debug.LogError("Convert date to day timestamp:" + Convert.ToInt64(st2.TotalDays));

//Method 3
double tS1 = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000);
Debug.LogError("Convert date to timestamp:" + tS1);

//Method 4
double tS2 = ((DateTime.UtcNow.Ticks - 621355968000000000) / 10000);
Debug.LogError("Convert date to timestamp:" + tS2);

Convert timestamp to date

 //Method 1
        DateTime startTime1 = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
        DateTime dt1 = startTime1.AddMilliseconds(1698766775000); //Incoming timestamp
        Debug.LogError("Time stamp to time:" + dt1);

        //Method Two
        DateTime startDateTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1, 0, 0, 0), TimeZoneInfo.Local);
        long targetTimeStamp = ((long)1698766775000 * 10000);
        TimeSpan targetTS = new TimeSpan(targetTimeStamp);
        DateTime targetDateTime = startDateTime.Add(targetTS);
        Debug.LogError("Timestamp to time:" + targetDateTime);

        //Method 3
        DateTime startTime3 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        DateTime dt3 = startTime3.AddTicks(1698766775000 * 10000); //Incoming timestamp
        Debug.LogError("Time stamp to time:" + dt3);

Convert timestamp to how long ago it was

 void Start()
{<!-- -->
    Debug.LogError(GetTimeLongAgo(20));
    Debug.LogError(GetTimeLongAgo(3000));
    Debug.LogError(GetTimeLongAgo(50000));
    Debug.LogError(GetTimeLongAgo(864000));
    Debug.LogError(GetTimeLongAgo(25920000));
    Debug.LogError(GetTimeLongAgo(61104000));
    
}
/// <summary>
    /// Convert the seconds timestamp to how long ago it was. Pass in timestamp t (t= current timestamp() - timestamp of the specified time)
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public string GetTimeLongAgo(double t)
    {<!-- -->
        string str = "";
        double num;
        if (t < 60)
        {<!-- -->
            str = string.Format("{0} seconds ago", t);
        }
        else if (t >= 60 & amp; & t < 3600)
        {<!-- -->
            num = Math.Floor(t / 60);
            str = string.Format("{0} minutes ago", num);
        }
        else if (t >= 3600 & amp; & t < 86400)
        {<!-- -->
            num = Math.Floor(t / 3600);
            str = string.Format("{0} hours ago", num);
        }
        else if (t > 86400 & amp; & t < 2592000)
        {<!-- -->
            num = Math.Floor(t / 86400);
            str = string.Format("{0} days ago", num);
        }
        else if (t > 2592000 & amp; & t < 31104000)
        {<!-- -->
            num = Math.Floor(t / 2592000);
            str = string.Format("{0} months ago", num);
        }
        else
        {<!-- -->
            num = Math.Floor(t / 31104000);
            str = string.Format("{0} years ago", num);
        }
        return str;
    }

Day of the week

 //Option 1
        string[] Day = new string[] {<!-- --> "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
        string week = Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString();
        Debug.LogError(week);

        //Option II
        Debug.LogError(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek));

        //third solution
        Debug.LogError(DateTime.Today.DayOfWeek.ToString());

Customized format time

DateTime dateTime = DateTime.Now;
string strNowTime = string.Format("{0:D}-{1:D}-{2:D}-{3:D}-{4:D}-{5:D}-{6:D}" , dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);

Debug.LogError(strNowTime);

Summary

There are actually many Chinese methods here. Some of the above knowledge is basically enough. Welcome to add in the comment area.