Trong bài viết trước tôi có giới thiệu thuộc tính Date.Now để lấy ngày giờ hiện tại trên computer, vậy giả sử ta chỉ muốn giờ không hiển thị ngày tháng thì phải làm như thế nào?
DateTime.Now.ToString("Format Time")
Là một thuộc tính(property) cơ bản của DateTime, nó được sử dụng để lấy ngày tháng hiện tại trên computer của bạn.
Khi kết hợp với hàm ToString với tham số là format giờ(time) thì nó sẽ hiển thị định dạng thời gian(time) theo cấu trúc được chỉ định.
Cú pháp - Syntax
DateTime.Now.ToString("Format Time")
Ví dụ - Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetCurrentTime
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\n");
Console.Write("Current Time: " + DateTime.Now.ToString("hh:mm:ss tt"));
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetCurrentTime
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\n");
Console.Write("Current Time: " + DateTime.Now.ToString("hh:mm:ss tt"));
Console.ReadLine();
}
}
}
Kết quả - Result
No comments:
Post a Comment