- Home /
Check what day of the week it is
So I have this day night cycle script that works on your computers time, and I was wondering if there's a way for it to know what day it is too? like Monday and stuff
the code: #pragma strict
var color1 : Color = Color.red;
var color2 : Color = Color.blue;
var Day : Light;
var Night : Light;
var dt = Date();
var timetext : GUIText;
function Update () {
var day = dt.Now.Day.ToString();
var month = dt.Now.Month.ToString();
var year = dt.Now.Year.ToString();
var hours = dt.Now.Hour.ToString();
var minutes = dt.Now.Minute.ToString();
if (parseInt(minutes) < 10) minutes = "0" + minutes;
var seconds = dt.Now.Second.ToString();
if(parseInt(seconds) < 10) seconds = "0" + seconds;
timetext.text = day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
if(parseInt(hours) > 19)
{
Day.enabled = false;
Night.enabled = true;
camera.backgroundColor = color2;
}
if(parseInt(hours) < 7)
{
Day.enabled = false;
Night.enabled = true;
}
if(parseInt(hours) < 19)
{
Day.enabled = true;
Night.enabled = false;
camera.backgroundColor = color1;
}
if(parseInt(hours) > 7)
{
Day.enabled = true;
Night.enabled = false;
}
}
Answer by HarshadK · Aug 11, 2014 at 02:32 PM
Here's a c# function: dateValue.DayOfWeek
If you want to code it then you can find required logic, formula with explanation here: Determination of the day of the week
It does work with javascript also.
This is the demo code for using it:
import System;
function Start () {
var dt: DateTime = new DateTime(2003, 5, 1);
Debug.Log(dt.DayOfWeek);
}
You need to use 'import System' specified at the top for this to work.
thanks it works c: heres a question though, what does the 2003, 5, 1 mean?
It is just the values for year, month, and day. Nothing much. :-)
Answer by Slipmike00 · Oct 07, 2018 at 11:28 AM
// Update is called once per frame void Update () {
day = (int)System.DateTime.Now.Day;
}
No, just no. The Day property returns the day of the month. Also this question has already been answered four years ago. The question specifically asked for the DayOfWeek
Answer by AdnanSiddique · Dec 30, 2021 at 07:06 AM
i want to detect is it a Sunday or not ? it is so simple, just follow the below line of code
if(System.DateTime.Now.DayOfWeek == System.DayOfWeek.Sunday)
Your answer
Follow this Question
Related Questions
Day Night Cycle Script Not Working? 1 Answer
How to make GUI Text appear after a certain amount of time 2 Answers
javascript System time and date? 3 Answers
Screen go Gray and only Cursor active OnTriggerEnter 1 Answer
Timed event Question 2 Answers