- Home /
 
Covnert system time into variables
Hi,
How would I convert Hours, Minute and Seconds into 3 variables from System.DateTime.Now ?
Thanks.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Griffo · Sep 01, 2012 at 10:11 AM
Found it ..
 #pragma strict
 
 var dt = Date();
 
 
 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;
 
 print(hours + " " + minutes + " " + seconds);
 
 }
 
              Answer by PProductions · Sep 01, 2012 at 01:54 PM
You can slit them up using the Now.Day() command on a time variable. similarly just use Now.Hour(), Now.Minute() to get the time.
 dt = Date();
 
 var day = dt.Now.Day.ToString();
 
 var month = dt.Now.Month.ToString();
 
 var year = dt.Now.Year.ToString();
 
               I added a switch statement the code to conver the numerical month into a nicly formatted string.
 switch(month) {
         case "1":
             formattedMonth = "January";
         break;
         case "2":
             formattedMonth = "Feburary";
         break;
         case "3":
             formattedMonth = "March";
         break;
         case "4":
             formattedMonth = "April";
         break;
         case "5":
             formattedMonth = "May";
         break;
         case "6":
             formattedMonth = "June";
         break;
         case "7":
             formattedMonth = "July";
         break;
         case "8":
             formattedMonth = "August";
         break;
         case "9":
             formattedMonth = "September";
         break;
         case "10":
             formattedMonth = "October";
         break;
         case "11":
             formattedMonth = "November";
         break;
         case "12":
             formattedMonth = "December";
         break;
     }
 
              Answer by Jamesies101 · Sep 02, 2012 at 12:35 PM
A quick google using unity 3d and Date() would of worked.
Sorry, wont do it again ;)
And shouldn't that be a comment not an answer ?
Your answer