- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[C#] Hướng dẫn lấy thời gian thực server time trên winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn các lấy thời gian từ Server, ở một số địa chỉ server time Internet Services.
[C#] Get Server Time Winform
Trong lập trình ứng dụng, nhiều lúc các bạn muốn hiển thị thời gian thực tế đúng trên server, vì mỗi máy client có thể thời gian chạy sẽ không giống nhau.
Các bạn có thể truy cập vào đường link bên dưới để xem các địa chỉ IP Server Time Internet Services.
Bài viết này mình sẽ hướng dẫn lấy thời gian đó và hiển thị lên Winform.
Lưu ý: là khi lấy thời gian server, các bạn lấy request thời gian phải cách nhau, sau khi bạn lấy thời gian server về có thể sử dụng Timer để chạy tiếp thời gian.
Danh sách các địa chỉ IP Time Server:
Source code C# get Server Time:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ServerTime
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnTime_Click(object sender, EventArgs e)
{
lbl_servertime.Text = ServerTime.getServerTime().ToLocalTime().ToString();
}
private void label1_Click(object sender, EventArgs e)
{
}
}
internal class ServerTime
{
private static string[] servers = { "132.163.96.1", "132.163.96.2", "129.6.15.28", "129.6.15.29" };
public static DateTime getServerTime()
{
foreach (string server in servers)
{
DateTime result = connectServer(server);
if (result > DateTime.MinValue)
return result;
}
return DateTime.MinValue;
}
private static DateTime connectServer(string host)
{
string time;
try
{
StreamReader read = new StreamReader(new TcpClient(host, 13).GetStream());
DateTime systemTime = DateTime.UtcNow;
time = read.ReadToEnd();
}
catch (Exception)
{
return DateTime.MinValue;
}
if (time.Substring(38, 9) != "UTC(NIST)") return DateTime.MinValue;
if (time.Substring(30, 1) != "0") return DateTime.MinValue;
int jd = int.Parse(time.Substring(1, 5));
int yr = int.Parse(time.Substring(7, 2));
int mo = int.Parse(time.Substring(10, 2));
int dy = int.Parse(time.Substring(13, 2));
int hr = int.Parse(time.Substring(16, 2));
int mm = int.Parse(time.Substring(19, 2));
int sc = int.Parse(time.Substring(22, 2));
if (jd < 15020) return DateTime.MinValue; //trước năm 1900
if (jd > 51544)
yr += 2000;
else
yr += 1900;
return new DateTime(yr, mo, dy, hr, mm, sc);
}
}
}
Thanks for watching!