C# 时间戳&&七天签到&&PlayerPrefs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEditor;
public class Test : MonoBehaviour {
	public Button[] signInButton=new Button[7];
	// Use this for initialization
	void Start () {
//		GetTimeStamp();
		SignInButtonManager ();
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void GetTimeStamp(){
//		DateTime nowTime = DateTime.Now;
//		string time = nowTime.ToString ();
//		DateTime ctie = Convert.ToDateTime (time);
//		Debug.Log ("现在时间:"+ctie);
//		DateTime lastTime = DateTime.Now.AddDays (-6);
//		Debug.Log ("过去时间:" + lastTime);
//		TimeSpan ts = ctie - lastTime;
//		double s = ts.TotalSeconds;
//		Debug.Log ("相差总秒数:" + s);
//		Debug.Log("天:"+(int)(s/(3600*24)));
//		Debug.Log("时:"+(int)(s%(3600*24)/3600));
//		Debug.Log ("分:" + (int)(s % 3600 / 60));
//		Debug.Log ("秒:" + (int)(s % 60));
		//ToUniversalTime()是转换为UTC时间,然后计算出来的是unix时间戳
		TimeSpan ts=DateTime.Now.ToUniversalTime()-new DateTime(1970,1,1);
		Debug.Log ((long)ts.TotalSeconds);
		//时间戳转化为datetime
		DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1, 1));
		DateTime targetTime = startTime.Add (ts);
		Debug.Log (targetTime);
		if (targetTime.ToString().Equals( DateTime.Now.ToString()) ) {
			Debug.Log ("yes:" + DateTime.Now);
						} else {
			Debug.Log ("No:" + DateTime.Now);
						}
//			long timeSpan = (DateTime.Now.ToUniversalTime().Ticks-621355964000000000)/10000000;
//			Debug.Log ("timeSpan:"+timeSpan);
//			long ltime = timeSpan*10000000;
//			DateTime dstart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1, 1,0,0,0));
//			TimeSpan toNow = new TimeSpan (ltime);
//	    	DateTime targetDt = dstart.Add (toNow);
//			if (targetDt == DateTime.Now) {
//				Debug.Log ("yes:" + timeSpan);
//			} else {
//				Debug.Log ("No:" + timeSpan);
//			}
//		Debug.Log ("targetDt:"+targetDt);
//	

//		Debug.Log ("time:"+dt);
//
//
//		Debug.Log ("相差天数:"+ts.Days);
//		Debug.Log ("相差小时:"+ts.Hours);
//		Debug.Log ("相差分钟:"+ts.Minutes);
//		Debug.Log ("相差秒:"+ts.Seconds);
	}
	/// 
	/// 获取当前时间并转化为字符串格式
	/// 
	/// The date time.
	private  string GetDateTime(){
		return DateTime.Now.ToString();
	}
	/// 
	/// 将字符串格式的日期转换为datetime格式
	/// 
	/// The to date time.
	/// Date time.
	private DateTime StringToDateTime(string dateTime){
		return Convert.ToDateTime (dateTime);
	}
	/// 
	/// 获取时间差值timespan
	/// 
	/// The time span.
	/// Start time.
	/// End time.
	private TimeSpan GetTimeSpan(DateTime startTime,DateTime endTime){
		return endTime - startTime;
	}
	/// 
	/// 根据时间戳获取离线时长
	/// 
	/// Time span.
	private void GetOfflineTime(TimeSpan timeSpan){
		int Days=timeSpan.Days;//离线的天数
		int Hours = timeSpan.Hours;//离线的小时,计算完天数后的剩余时间,相当于时间对于天数取余数后的时间
		int Minutes = timeSpan.Minutes;//分钟,同上
		int Seconds = timeSpan.Seconds;//秒数,同上
		double  TotalSeconds=Math.Floor(timeSpan.TotalSeconds) ;//向下取整获得总秒数eg。3.9=》3
		//		Math.Round() 四舍五入,偶数逢5不加1,奇数加1 eg。3.5=》4 ;4.5=》4; Math.Ceiling() 向上取整eg。3.1=》4
	}

	private bool SignIn(TimeSpan timeSpan){
		int Days=timeSpan.Days;
		if (Days >= 1) {
			return true;
		} else {
			return false;
		}
	}

	private void ToSignIn(){
		if (PlayerPrefs.HasKey ("SignIn")) {
			int time = PlayerPrefs.GetInt ("SignIn") + 1;
			PlayerPrefs.SetInt ("SignIn", time);
		} else {
			PlayerPrefs.SetInt ("SignIn", 1);

		}

		PlayerPrefs.SetString ("SingIntime", GetDateTime ());

		SignInButtonManager ();
	}

	private void SignInButtonManager(){
		for (int i = 0; i 

 

你可能感兴趣的:(时间戳)