Trong bài trước chúng ta đã tìm hiểu về thuộc tính Today của DateTime, bài viết này chúng ta sẽ tìm hiểu về cách hiển thị ngày tháng theo một số định dạng được chỉ định.
DateTime.Today.ToString("Date Format")
Tương tự với thuộc tính Now thuộc tính Today cũng sữ dụng ToString để định dạng format hiển thị của ngày tháng.
Cú pháp - Syntax
DateTime.Today.ToString("Date Format")
Ví dụ - Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetDateTimeCurrent
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\n\n");
// Display the date in the default (general) format.
Console.WriteLine("Current day: " + DateTime.Today.ToString());
// Display the date in a variety of formats.
Console.WriteLine("Current format day: " + DateTime.Today.ToString("d"));
Console.WriteLine("Current format day: " + DateTime.Today.ToString("D"));
Console.WriteLine("Current format day: " + DateTime.Today.ToString("g"));
// Display the date in a variety of formats.
Console.WriteLine("Current format day: " + DateTime.Today.ToString("dd-MM-yyyy"));
Console.WriteLine("Current format day: " + DateTime.Today.ToString("MM-dd-yyyy"));
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetDateTimeCurrent
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\n\n");
// Display the date in the default (general) format.
Console.WriteLine("Current day: " + DateTime.Today.ToString());
// Display the date in a variety of formats.
Console.WriteLine("Current format day: " + DateTime.Today.ToString("d"));
Console.WriteLine("Current format day: " + DateTime.Today.ToString("D"));
Console.WriteLine("Current format day: " + DateTime.Today.ToString("g"));
// Display the date in a variety of formats.
Console.WriteLine("Current format day: " + DateTime.Today.ToString("dd-MM-yyyy"));
Console.WriteLine("Current format day: " + DateTime.Today.ToString("MM-dd-yyyy"));
Console.ReadLine();
}
}
}
Kết quả - Result
No comments:
Post a Comment