LoginSignup
1
2

More than 5 years have passed since last update.

unity > System.DateTime > 日曜日からの日数を計算する > nowとsundayを取得 / int daysdiff = now.Subtract (sunday).Days;

Last updated at Posted at 2015-09-12
動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5

現在時刻が前の日曜日から何日目かを知りたい。

v0.1 @ github

MyScript.cs
using UnityEngine;
using System.Collections;
using System; // for TimeSpan

/*
 * v0.1 2015/09/12
 *   - get days from sunday
 */

public class MyScript : MonoBehaviour {

    System.DateTime getSundayH00M00S00(System.DateTime dt) {
        System.DateTime res = dt;
        dt -= new TimeSpan ((int)dt.DayOfWeek, dt.Hour, dt.Minute, dt.Second);
        return dt;
    }

    void Test_weekly() {
        System.DateTime now = System.DateTime.Now;
        System.DateTime sunday = getSundayH00M00S00 (now);

        int daysdiff = now.Subtract (sunday).Days;

        Debug.Log ("now: " + now.ToString ());
        Debug.Log ("sunday: " + sunday.ToString ());
        Debug.Log ("days: " + daysdiff);
    }

    void Start () {
        Test_weekly ();
    }

}

Main_unity_-_150912-weeklyGraph_-_PC__Mac___Linux_Standalone__Personal_.jpg

きちんと6日が取得できた。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2