Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1

    Cho em hỏi làm sao chuyển định dạng giờ từ h:m:s sang hh:mm:ss

    Đây là code của em
    Mã:
     public class time
        {
            public int gio { get; set; }
            public int phut { get; set; }
            public int giay { get; set; }
           
            public void print12()
            {
                Console.WriteLine("Dạng 12 giờgày: ");
                if (this.gio >= 12)
                {
                    Console.WriteLine("{0}:{1}:{2} PM", this.gio - 12, this.phut, this.giay);
                }
                else
                {
                    Console.WriteLine("{0}:{1}:{2} AM", this.gio, this.phut, this.giay);
                    Console.ReadKey();
                }
            }
            public void print24()
            {
                Console.WriteLine("Dạng 24 giờgày: ");
                Console.WriteLine("{0}:{1}:{2}", this.gio, this.phut, this.giay);
            }
            public void Tanggiay(int s)
            {
                this.giay = s + 1;
    
            }
            public void Tangphut(int p)
            {
                if (this.giay == 60)
                {
                    this.phut = p + 1;
    
                    this.giay = 0;
                }
               
            }
            public void Tanggio(int h)
            {
                if (this.phut == 60)
                {
                    this.gio = h + 1;
                    this.phut = 0;
                   
                    if (this.gio == 24)
                    {
                        this.gio = 0;
                    }
    
                }
            }
            class program
            {
                static void Main(string[] args)
                {
                    Console.OutputEncoding = Encoding.UTF8;
                     time TG = new time();
                    Console.Write("Nhập giờ: ");
                    TG.gio = int.Parse(Console.ReadLine());
                    Console.Write("Nhập phút: ");
                    TG.phut = int.Parse(Console.ReadLine());
                    Console.Write("Nhập giây: ");
                    TG.giay = int.Parse(Console.ReadLine());
                    TG.print12();
                    TG.print24();
                    Console.ReadKey();
                    for (int i = 1; i <= 5; i++)
                    {
                        TG.Tanggiay(TG.giay);
                        TG.Tangphut(TG.phut);
                        TG.Tanggio(TG.gio);
                        Console.WriteLine("Thời gian sau khi tăng {0} giây là: ", i);
                        TG.print12();
                        TG.print24();
                    }
                        
                   
    
                }
            }
        }
    }

  2. #2
    DateTime.Now.ToString("hh:mm:ss")

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •