我是靠谱客的博主 干净大叔,这篇文章主要介绍C# 时间戳&&七天签到&&PlayerPrefs,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部