facebook twitter hatena line email

「Unity/Csharp/日時」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(タイムゾーン付き日付)
 
(同じ利用者による、間の12版が非表示)
行1: 行1:
 +
 +
==日時表示==
 +
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // HHにすると24時間表記となる
 +
2019-02-28 22:57:20
 +
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); // hhにすると12時間表記となる
 +
2019-02-28 10:57:20
 +
 +
using System;
 +
string GetYmdHm() {
 +
string h = ("0" + System.DateTime.Now.Hour.ToString ());
 +
string i = ("0" + System.DateTime.Now.Minute.ToString ());
 +
return System.DateTime.Now.Year.ToString() + "-" +
 +
System.DateTime.Now.Month.ToString() + "-" +
 +
System.DateTime.Now.Day.ToString() + " " +
 +
h.Substring(h.Length - 2, 2) + ":" +
 +
i.Substring(i.Length - 2, 2);
 +
}
 +
 +
==日付入力==
 +
var dt = new DateTime( 2022, 1, 26, 10, 20, 30 );
 +
Debug.Log(dt.ToString()); // 1/26/2022 10:20:30 PM
 +
 
==時間表示==
 
==時間表示==
 
  using System;
 
  using System;
行4: 行26:
 
  > 16:12:10
 
  > 16:12:10
  
日付フォーマット一覧
+
==日付フォーマット一覧==
 
https://msdn.microsoft.com/ja-jp/library/k494fzbf(v=VS.80).aspx
 
https://msdn.microsoft.com/ja-jp/library/k494fzbf(v=VS.80).aspx
 +
d :08/17/2000
 +
D :Thursday, August 17, 2000
 +
f :Thursday, August 17, 2000 16:32
 +
F :Thursday, August 17, 2000 16:32:32
 +
g :08/17/2000 16:32
 +
G :08/17/2000 16:32:32
 +
m :August 17
 +
r :Thu, 17 Aug 2000 23:32:32 GMT
 +
s :2000-08-17T16:32:32
 +
t :16:32
 +
T :16:32:32
 +
u :2000-08-17 23:32:32Z
 +
U :Thursday, August 17, 2000 23:32:32
 +
y :August, 2000
 +
dddd, MMMM dd yyyy :Thursday, August 17 2000
 +
ddd, MMM d "'"yy :Thu, Aug 17 '00
 +
dddd, MMMM dd :Thursday, August 17
 +
M/yy :8/00
 +
dd-MM-yy :17-08-00
 +
 +
==unixtimeを取得==
 +
var now = DateTime.UtcNow;
 +
long unixtime = (long)(now - new DateTime(1970, 1, 1)).TotalSeconds;
 +
 +
==unixtimeとDatetime変換==
 +
https://gist.github.com/YuukiTsuchida/06ca3a1f0baf755651b0#file-unixtime
 +
 +
==秒を時分秒へ==
 +
<pre>
 +
int sec = 3666;
 +
string his = string.Format("{0:00}:{1:00}:{2:00}", (sec / 60 / 60), (sec / 60) % 60, sec % 60);
 +
Debug.Log("his=" + his);  // 01:01:06
 +
</pre>
 +
参考:https://cms.shise.net/2014/10/sec-num-format/
  
 +
==差分秒数==
 +
12:30ぐらいだったとき、300秒と出る
 +
<pre>
 +
static DateTime targetDateTime = new DateTime(2022, 1, 26, 12, 35, 0);
 +
TimeSpan ts = targetDateTime - DateTime.Now;
 +
Debug.Log("diff sec=" + ts.TotalSeconds);
 +
</pre>
  
    * d :08/17/2000
+
==タイムゾーン付き日付==
    * D :Thursday, August 17, 2000
+
<pre>
    * f :Thursday, August 17, 2000 16:32
+
year = 2022;
    * F :Thursday, August 17, 2000 16:32:32
+
month = 2;
    * g :08/17/2000 16:32
+
day = 28;
    * G :08/17/2000 16:32:32
+
hour = 12;
    * m :August 17
+
minute = 59;
    * r :Thu, 17 Aug 2000 23:32:32 GMT
+
second = 30;
    * s :2000-08-17T16:32:32
+
var dateTime = new DateTimeOffset(
    * t :16:32
+
                year,
    * T :16:32:32
+
                month,
    * u :2000-08-17 23:32:32Z
+
                day,
    * U :Thursday, August 17, 2000 23:32:32
+
                hour,
    * y :August, 2000
+
                minute,
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
+
                second,
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
+
                new TimeSpan(9, 0, 0)
    * dddd, MMMM dd :Thursday, August 17
+
                );
    * M/yy :8/00
+
Debug.Log(dateTime.ToString("yyyy-MM-dd HH:mm:ss")); // 2022/2/28 12:59:30
    * dd-MM-yy :17-08-00
+
// Utcに変換
 +
dateTime = dateTime.ToUniversalTime();
 +
Debug.Log(dateTime.ToString("yyyy-MM-dd HH:mm:ss")); // 2022/2/28 3:59:30
 +
// 現在と比較
 +
TimeSpan ts = dateTime - DateTime.UtcNow; // 過ぎてたら、マイナスになる
 +
// 現在との秒数差分
 +
Debug.Log(ts.TotalSeconds); 16.383
 +
</pre>

2022年2月9日 (水) 13:22時点における最新版

日時表示

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // HHにすると24時間表記となる
2019-02-28 22:57:20
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); // hhにすると12時間表記となる
2019-02-28 10:57:20
using System;
string GetYmdHm() {
		string h = ("0" + System.DateTime.Now.Hour.ToString ());
		string i = ("0" + System.DateTime.Now.Minute.ToString ());
		return System.DateTime.Now.Year.ToString() + "-" +
			System.DateTime.Now.Month.ToString() + "-" +
			System.DateTime.Now.Day.ToString() + " " +
			h.Substring(h.Length - 2, 2) + ":" +
			i.Substring(i.Length - 2, 2);
}

日付入力

var dt = new DateTime( 2022, 1, 26, 10, 20, 30 );
Debug.Log(dt.ToString()); // 1/26/2022 10:20:30 PM

時間表示

using System;
DateTime.Now.ToString("T")
> 16:12:10

日付フォーマット一覧

https://msdn.microsoft.com/ja-jp/library/k494fzbf(v=VS.80).aspx

d :08/17/2000
D :Thursday, August 17, 2000
f :Thursday, August 17, 2000 16:32
F :Thursday, August 17, 2000 16:32:32
g :08/17/2000 16:32
G :08/17/2000 16:32:32
m :August 17
r :Thu, 17 Aug 2000 23:32:32 GMT
s :2000-08-17T16:32:32
t :16:32
T :16:32:32
u :2000-08-17 23:32:32Z
U :Thursday, August 17, 2000 23:32:32
y :August, 2000
dddd, MMMM dd yyyy :Thursday, August 17 2000
ddd, MMM d "'"yy :Thu, Aug 17 '00
dddd, MMMM dd :Thursday, August 17
M/yy :8/00
dd-MM-yy :17-08-00

unixtimeを取得

var now = DateTime.UtcNow;
long unixtime = (long)(now - new DateTime(1970, 1, 1)).TotalSeconds;

unixtimeとDatetime変換

https://gist.github.com/YuukiTsuchida/06ca3a1f0baf755651b0#file-unixtime

秒を時分秒へ

int sec = 3666;
string his = string.Format("{0:00}:{1:00}:{2:00}", (sec / 60 / 60), (sec / 60) % 60, sec % 60);
Debug.Log("his=" + his);  // 01:01:06

参考:https://cms.shise.net/2014/10/sec-num-format/

差分秒数

12:30ぐらいだったとき、300秒と出る

static DateTime targetDateTime = new DateTime(2022, 1, 26, 12, 35, 0);
TimeSpan ts = targetDateTime - DateTime.Now;
Debug.Log("diff sec=" + ts.TotalSeconds);

タイムゾーン付き日付

year = 2022;
month = 2;
day = 28;
hour = 12;
minute = 59;
second = 30;
var dateTime = new DateTimeOffset(
                year,
                month,
                day,
                hour,
                minute,
                second,
                new TimeSpan(9, 0, 0)
                );
Debug.Log(dateTime.ToString("yyyy-MM-dd HH:mm:ss")); // 2022/2/28 12:59:30
// Utcに変換
dateTime = dateTime.ToUniversalTime();
Debug.Log(dateTime.ToString("yyyy-MM-dd HH:mm:ss")); // 2022/2/28 3:59:30
// 現在と比較
TimeSpan ts = dateTime - DateTime.UtcNow; // 過ぎてたら、マイナスになる
// 現在との秒数差分
Debug.Log(ts.TotalSeconds); 16.383