Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Check if the time is correct.

Discussion in 'Scripting' started by caseyboundless, Jul 4, 2014.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I have a simple script that tells me the real time on my computer. But i dont how to make am or pm specific in the if statement.

    webplayer: https://dl.dropboxusercontent.com/u/56545886/clocktest/clocktest.html

    Updated working script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. public class CheckTime : MonoBehaviour {
    5.  
    6.  
    7.  
    8.  
    9.     public GameObject amWord; // just am word for dispaly
    10.     public GameObject pmWord; // just pm word for display
    11. //    public dfLabel dfLabelCurrentTime; // df label that is updated as current time
    12.     public dfLabel currentHour;
    13.     public dfLabel currentMinutes;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.    
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.         //DateTime currentTime = DateTime.Now;
    24.  
    25.         TimeSpan currentTime = DateTime.Now.TimeOfDay;
    26.         currentMinutes.Text = currentTime.Minutes.ToString ();
    27.  
    28.  
    29.         if (currentTime.Hours == 1 || currentTime.Hours <= 11)
    30.         {
    31.             amWord.SetActive (true);
    32.             pmWord.SetActive (false);
    33.             currentHour.Text = currentTime.Hours.ToString (); // update the current time with df label
    34.             currentMinutes.Text = currentTime.Minutes.ToString (); // update the current time with df label
    35.        
    36.         }
    37.  
    38.         else
    39.         {
    40.             pmWord.SetActive(true);
    41.             amWord.SetActive(false);
    42.  
    43.         }
    44.    
    45.  
    46.         if (currentTime.Hours == 15)
    47.         {
    48.             currentHour.Text = "3";
    49.         }
    50.  
    51.         if (currentTime.Hours == 16)
    52.         {
    53.             currentHour.Text = "4";
    54.         }
    55.  
    56.         if (currentTime.Hours == 17)
    57.         {
    58.             currentHour.Text = "5";
    59.         }
    60.  
    61.         if (currentTime.Hours == 18)
    62.         {
    63.             currentHour.Text = "6";
    64.         }
    65.  
    66.  
    67.         if (currentTime.Hours == 19)
    68.         {
    69.             currentHour.Text = "7";
    70.         }
    71.  
    72.         if (currentTime.Hours == 20)
    73.         {
    74.             currentHour.Text = "8";
    75.         }
    76.  
    77.         if (currentTime.Hours == 21)
    78.         {
    79.             currentHour.Text = "9";
    80.         }
    81.  
    82.         if (currentTime.Hours == 22)
    83.         {
    84.             currentHour.Text = "10";
    85.         }
    86.  
    87.         if (currentTime.Hours == 23)
    88.         {
    89.             currentHour.Text = "11";
    90.         }
    91.  
    92.         if (currentTime.Hours == 24)
    93.         {
    94.             currentHour.Text = "12";
    95.         }
    96.  
    97.  
    98.  
    99.  
    100.  
    101.         if (currentTime.Hours >= 6 && currentTime.Minutes == 00 )
    102.         {
    103.             Debug.Log(" hey its 6:00 am or passed you can use leaderbaord and weigh fish to count ");
    104.         }
    105.    
    106.         if (currentTime.Hours == 20 && currentTime.Minutes == 00 )
    107.         {
    108.             Debug.Log(" hey its 8:00 pm or paased time to close leaderboard ");
    109.         }
    110.  
    111.    
    112.     }
    113. }
    114.  
























    old script


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. public class CheckTime : MonoBehaviour {
    5.  
    6.  
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         //DateTime currentTime = DateTime.Now;
    18.  
    19.         TimeSpan currentTime = DateTime.Now.TimeOfDay;
    20.  
    21.  
    22.     if (currentTime.Hours == 1 && currentTime.Minutes == 34 )
    23.         {
    24.             Debug.Log(" hey its 1:34 am ");
    25.         }
    26.  
    27.  
    28.  
    29.  
    30.     }
    31. }
    32.  
     
    Last edited: Jul 6, 2014
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Hours values over 12 are PM.
     
  3. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    So 1 would be 1 am and lets say 14 is 2pm? My range is 1-24?
     
    Last edited: Jul 4, 2014
  4. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    EDITED

    I think this will work. Its accurate just tested on my ipad and computer.

    webplayer demo of clock : https://dl.dropboxusercontent.com/u/56545886/clocktest/clocktest.html



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. public class CheckTime : MonoBehaviour {
    5.  
    6.  
    7.  
    8.  
    9.     public GameObject amWord; // just am word for dispaly
    10.     public GameObject pmWord; // just pm word for display
    11.     public dfLabel dfLabelCurrentTime; // df label that is updated as current time
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         //DateTime currentTime = DateTime.Now;
    23.  
    24.         TimeSpan currentTime = DateTime.Now.TimeOfDay;
    25.         dfLabelCurrentTime.Text = currentTime.ToString (); // update the current time with df label
    26.  
    27.  
    28.         if (currentTime.Hours == 1 || currentTime.Hours <= 11)
    29.         {
    30.             amWord.SetActive (true);
    31.             pmWord.SetActive (false);
    32.      
    33.         }
    34.  
    35.         else
    36.         {
    37.             pmWord.SetActive(true);
    38.             amWord.SetActive(false);
    39.  
    40.         }
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.  
    51.         if (currentTime.Hours >= 6 && currentTime.Minutes == 00 )
    52.         {
    53.             Debug.Log(" hey its 6:00 am or passed you can use leaderbaord and weigh fish to count ");
    54.         }
    55.  
    56.         if (currentTime.Hours == 20 && currentTime.Minutes == 00 )
    57.         {
    58.             Debug.Log(" hey its 8:00 pm or paased time to close leaderboard ");
    59.         }
    60.  
    61.  
    62.     }
    63. }
    64.  
     
    Last edited: Jul 4, 2014
  5. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Yup, though it says "14:08 pm". Should subtract 12 if PM when displaying.
     
  6. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Got it to work check wb.
     
    Last edited: Jul 6, 2014
  7. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Actual when the clock is like 1:03 its display 1:3???? Any ideas?