DateTime does not contain a definition for `hour'

baljeet1981

New member
Joined
May 25, 2017
Messages
1
Programming Experience
Beginner
using System.Collections;
using UnityEngine;


using UnityEngine.UI;
using System;


public class ClockDigital : MonoBehaviour {
    private Text textClock;
    
	void Start () {
        textClock = GetComponent<Text>();
            }


    
    void Update() {
        DateTime time = DateTime.Now;
        string hour = LeadingZero(time.hour);
        string minute = LeadingZero(time.minute);
        string second = LeadingZero(time.second);


        textClock.text = hour + ":" + minute + ":" + second;
    }
            string LeadingZero(int n) {
        return n.ToString().PadLeft(2, '0');
    }
}

When I try to execute, I get the following errors:

1. ClockDigital.cs(17,40): error CS1061: Type `System.DateTime' does not contain a definition for `hour' and no extension method `hour' of type `System.DateTime' could be found. Are you missing an assembly reference?

2. ClockDigital.cs(18,42): error CS1061: Type `System.DateTime' does not contain a definition for `minute' and no extension method `minute' of type `System.DateTime' could be found. Are you missing an assembly reference?

3. ClockDigital.cs(19,42): error CS1061: Type `System.DateTime' does not contain a definition for `second' and no extension method `second' of type `System.DateTime' could be found. Are you missing an assembly reference?
 
This is what you need:

C#:
DateTime currentDateTime = DateTime.Now; 

int currentHour = currentDateTime.Hour;

So we declared a DateTime variable that gets the time. Then the second line is the line that you need.

Hope that helps
 
Back
Top Bottom