Skip to content

Instantly share code, notes, and snippets.

@priesdelly
Last active February 24, 2019 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priesdelly/29c260bb60b66976fd88018ef3c64029 to your computer and use it in GitHub Desktop.
Save priesdelly/29c260bb60b66976fd88018ef3c64029 to your computer and use it in GitHub Desktop.
Date, Datetime show in other culture info
using System;
using System.Collections.Generic;
using System.Text;
// นำเข้าคลาส Globalization โดยเรียกผ่านเนมสเปช System.Globalization
// เพื่อให้สามารถเรียกใช้คลาส CultureInfo() ได้
using System.Globalization;
namespace DateCultureCSharp
{
class Program
{
static void Main(string[] args)
{
//ประกาศ Cultureinfo ของแต่ละแบบที่ต้องการ
CultureInfo thCulture = new CultureInfo("th-TH");
CultureInfo usCulture = new CultureInfo("en-US");
//ประกาศ DateTime เพื่อมาเป็นเวลาปัจจุบัน
DateTime dtNow = new DateTime();
dtNow = DateTime.Now;
//แสดงแค่ปี พ.ศ. กับ ค.ศ.
Console.WriteLine(dtNow.ToString("yyyy", thCulture));
Console.WriteLine(dtNow.ToString("yyyy", usCulture));
//ในส่วนคำว่า "yyyy" เป็นการกำหนดฟอร์แมตการแสดงผลว่า แสดงแค่ปีจำนวน 4 หลัก นะครับ
//โดยผมได้ใส่ Culture ต่างๆ ที่ผมต้องการเป็นพารามิเตอร์
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment