Làm thế nào để hiển thị ngày tháng trong C-Sharp với nhiều định dạng khác nhau? (How to Display the Date in Various Formats in C-Sharp?)

Đây là một bài tập, ví dụ, thường gặp khi học C-Sharp tới phần kiểu dữ liệu dạng ngày tháng (Date). Bài ví dụ này sẽ mô tả cách để hiển thị ngày tháng (Date) trong C-Sharp với nhiêu định dạng khác nhau ví dụ như DD/MM/YYYY, YYYY/MM/DD, ...



C-Sharp Date Example:
------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpExample
{
    class DisplayDateTime
    {

        static void Main(string[] args)
        {

            DateTime date = new DateTime(2013, 6, 23);

            Console.WriteLine("Some Date Formats : ");
            Console.WriteLine("Date and Time:  {0}", date);
            Console.WriteLine(date.ToString("yyyy-MM-dd"));
            Console.WriteLine(date.ToString("dd-MMM-yy"));
            Console.WriteLine(date.ToString("M/d/yyyy"));
            Console.WriteLine(date.ToString("M/d/yy"));
            Console.WriteLine(date.ToString("MM/dd/yyyy"));
            Console.WriteLine(date.ToString("MM/dd/yy"));
            Console.WriteLine(date.ToString("yy/MM/dd"));
            Console.Read();


        }


    }
}

---------------------------------







No comments:

Post a Comment