Free advanced time script

Since this community helped me out in fixing the errors and stuff, …here!
It has complete dates with even the number of days for each individual month and even for leap years. (Leap years - Well up to 2020, but you can add others in)
If used please give credit
EDIT: Oh and almost forgot the var speedtime doesn’t work since I couldn’t figure out how to use it to speed up time :confused:

//========================================KAYDEN=====================================
//===================================DRAPIK================================================
//Upcoming MMORPG facebook.com/Drapik

var Year = 2012;
var Month = 1;
//var Week = 0;
var Day = 1;
var Hour = 0;
var Minute = 0;
var seconds : float = 1;
var speedtime = 0;

//Private vars
private var YearCalculator = 2012;
private var MonthCalculator = 1;
//private var WeekCalculator = 0;
private var DayCalculator = 1;
private var HourCalculator = 0;
private var MinuteCalculator = 0;
private var SecondCalculator = 0;

function Start () {

}											
  // Seconds to Minutes
function Update() {
  seconds += Time.deltaTime;
  if (seconds >= 60){
        seconds=0;
        Minute +=1;
        }
        
 //Minutes to Hours
 Minute += Time.deltaTime;
  if (Minute >= 60){
        Minute=0;
        Hour +=1;
        }
 
        //Hours to Day
 Hour += Time.deltaTime;
  if (Hour >= 24){
        Hour=0;
        Day +=1;
        }
       
       //Day to Month
       
 Day += Time.deltaTime;
 //Jan
  if (Month ==1  Day ==32){
        Day=1;
        Month +=1;
        }
        //Feb
        //If leap year
        if (Month ==2  Day ==30  Year ==2012  || Month ==2  Day ==30  Year ==2016 || Month ==2  Day ==30  Year ==2020){ 

        Day=1;
        Month +=1; 
        }
        //If not a leap year
  else if (Month ==2  Day ==29  Year ==2013 || Month ==2  Day ==29  Year ==2014 || Month ==2  Day ==29  Year ==2015){ 
        Day=1;
        Month +=1;
        }      
        //March
        if (Month ==3  Day ==32){
        Day=1;
        Month +=1;
        }
        //April
        if (Month ==4  Day ==31){
        Day=1;
        Month +=1;
        }
        //May
        if (Month ==5  Day ==32){
        Day=1;
        Month +=1;
        }
        //June
        if (Month ==6  Day ==31){
        Day=1;
        Month +=1;
        }
        //July
        if (Month ==7  Day ==32){
        Day=1;
        Month +=1;
        }
      //August
      if (Month ==8  Day ==32){
        Day=1;
        Month +=1;
        }
        //September
        if (Month ==9  Day ==31){
        Day=1;
        Month +=1;
        }
        //October
        if (Month ==10  Day ==32){
        Day=1;
        Month +=1;
        }
        //November
        if (Month ==11  Day ==31){
        Day=1;
        Month +=1;
        }
        //December
        if (Month ==12  Day ==32){
        Day=1;
        Month +=1;
        }
       
   
       
   //Month to Year
 Month += Time.deltaTime;
  if (Month > 12){
        Month=1;
        Year +=1;
        }     
}

Wow thanks, great script. Also to speed up Unity’s time, change the variable Time.timeScale, it’s normalized so Time.timeScale = 2 would be twice as fast.

You’re adding Time.deltaTime (a number of seconds) to Minutes, Days, Hours, and Months, all of which are integers (so they’ll never change).

Arrays make things so, so, so much easier to work with:

var endDays : int[] = [32,29,32...];

// 1 because zero-indexed arrays
if ( month == 1  day == 29  year / 4.0 == year / 4  year != 2000 ) {
  // 'don't advance'
} else {
  if ( day >= endDays[ month ] ) {
    day = 1;
    month = (month+1) % 12;
  }
}

Thanks, haven’t really read about arrays yet.

@Cat did work :confused:

And how exactly is this better than the System classes?

public var startYear:int;
public var startMonth:int;
public var startDay:int;
public var inGameTimeFactor:int = 1;

private var startTime:System.DateTime;

function Awake(){
	startTime = new System.DateTime(startYear, startMonth, startDay);
}

function Update(){
	// Hey we have a real date we can do all kinds of nice things
	Debug.Log(GetTime().Hour);
	Debug.Log(GetTime().ToString("F"));
	Debug.Log(GetTime() < new System.DateTime(2012, 12, 12) ? "SAFE" : "DEAD");
	
}

function GetTime():System.DateTime {
	return startTime + new System.TimeSpan(System.TimeSpan.TicksPerSecond * Time.timeSinceLevelLoad * inGameTimeFactor);
}

Easier for beginners to understand…

Not really. You have an awful lot of excess code, that doesn’t actually work right (the stuff about leap years, for example), when you could simply do something like:

function Start () {
	var time = System.DateTime.Now;
	Debug.Log (time.Year + " " + time.Month + " " + time.Hour + " " + time.Minute + " " + time.Second);
}

JohnnyA’s code is just setting up a DateTime object so you can have it start at any time (“Now” gives the system time), plus some “fancy” stuff, but the basic concept of DateTime is quite simple, as you can see. I don’t want to sound discouraging, because learning programming is great, but this is a pretty good example of unnecessarily reinventing the wheel. It’s always a good idea to become familiar with your available resources so you don’t spend a lot of time working on things that already exist.

–Eric

+1

I probably should have posted a simpler example, I was trying to preempt some questions (“but what if I don’t want to use the current date” or “what if my game time moves at a different rate” :))

You can easily change the calculations or the integers in the var section