NEWS

[C#] Hướng dẫn lấy danh sách múi giờ Timezone UTC trên thế giới trên Winform

[C#] Hướng dẫn lấy danh sách múi giờ Timezone UTC trên thế giới trên Winform
Đăng bởi: Thảo Meo - Lượt xem: 4696 19:13:30, 25/04/2022DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách lấy tất cả các múi giờ trên thế giới Timezone UTC trên Winform C#.

[C#] Get Timezone UTC winform

Dưới đây là giao diện demo ứng dụng:

tim_zone_hanoi

Ở hình trên, các bạn thấy mình hiển thị hết danh sách các múi giờ vào trong listbox trên c#.

Các dữ liệu này là mình lấy từ trong thư viện của Winform C#.

Bạn chỉ cần gọi hàm sau là nó trả về cho bạn danh sách các múi giờ:

TimeZoneInfo.GetSystemTimeZones()

Code nguyên chương trình đơn giản chỉ cần vài dòng dưới đây:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TimeZoneInfoSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReadOnlyCollection<TimeZoneInfo> collection = TimeZoneInfo.GetSystemTimeZones();
            this.listBox.Items.AddRange(collection.ToArray());
            // set default time zone Hà nội, việt nam
            var index = collection.ToList().FindIndex(x => x.DisplayName.ToLower().Contains("hanoi"));
            this.listBox.SetSelected(index, true);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeZoneInfo timeZoneInfo = (TimeZoneInfo)this.listBox.SelectedItem;
            this.currentTimeZoneLabel.Text = $"{this.listBox.Text} : {TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo).ToString()}";
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn lấy danh sách múi giờ Timezone UTC trên thế giới trên Winform
Đăng bởi: Thảo Meo - Lượt xem: 4696 19:13:30, 25/04/2022DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.