概述
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);
}
/// <summary>
/// 获取当前时间并转化为字符串格式
/// </summary>
/// <returns>The date time.</returns>
private
string GetDateTime(){
return DateTime.Now.ToString();
}
/// <summary>
/// 将字符串格式的日期转换为datetime格式
/// </summary>
/// <returns>The to date time.</returns>
/// <param name="dateTime">Date time.</param>
private DateTime StringToDateTime(string dateTime){
return Convert.ToDateTime (dateTime);
}
/// <summary>
/// 获取时间差值timespan
/// </summary>
/// <returns>The time span.</returns>
/// <param name="startTime">Start time.</param>
/// <param name="endTime">End time.</param>
private TimeSpan GetTimeSpan(DateTime startTime,DateTime endTime){
return endTime - startTime;
}
/// <summary>
/// 根据时间戳获取离线时长
/// </summary>
/// <param name="timeSpan">Time span.</param>
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 <signInButton.Length; i++) {
signInButton [i].onClick.AddListener (ToSignIn);
if (PlayerPrefs.HasKey ("SignIn")) {
if (i < PlayerPrefs.GetInt ("SignIn")) {
signInButton [i].transform.GetChild (1).gameObject.SetActive (true);
} else {
signInButton [i].transform.GetChild (1).gameObject.SetActive (false);
}
} else {
signInButton [i].transform.GetChild (1).gameObject.SetActive (false);
}
signInButton [i].interactable = false;
}
if (PlayerPrefs.HasKey ("SignIn")) {
Debug.Log ("HasyKey");
int index = PlayerPrefs.GetInt ("SignIn");
if (SignIn (DateTime.Now - StringToDateTime (PlayerPrefs.GetString ("SingIntime")))) {
signInButton [index].interactable = true;
}
} else {
signInButton [0].interactable = true;
}
}
}
public class Tool{
[MenuItem("Tool/ClearAllDate")]
static void ClearData(){
PlayerPrefs.DeleteAll ();
}
}
最后
以上就是干净大叔为你收集整理的C# 时间戳&&七天签到&&PlayerPrefs的全部内容,希望文章能够帮你解决C# 时间戳&&七天签到&&PlayerPrefs所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复