VANHIEP.NET - Làm web giá rẻ - Thiết Kế Website - Thiết Kế Ứng Dụng Mobile

Format ngày tháng trong javacript

var date_droadcasting = new Date(1000*element.date_droadcasting);
date_droadcasting = new Intl.DateTimeFormat(['ban', 'id']).format(date_droadcasting);
            

ouput : 20/10/2020

Thay element.date_droadcasting bằng int date từ server trả về

var options = {
                hour: 'numeric', minute: 'numeric',
                timeZone: 'Asia/Bangkok',
              };
var time = new Date(1000*element.time_droadcasting)
time = new Intl.DateTimeFormat('en-AU', options).format(time);

Đoạn code trên thay en-Au bằng en-GB thì sẽ thay đổi kiểu giờ 0h đến 23h

Tương tự cho giờ

 

Trường hợp format YYYY-MM-DD

var date_droadcasting = new Date(1000 * parseInt(data_int_date));
date_droadcasting= date_droadcasting.toISOString().split('T')[0];

Thay data_int_date bằng dữ liệu int của bạn

Ouput có dạng : 2022/1/22

 

Trường hợp format giờ : 17:00:05 

var time = new Date(1000*data_int)
time = time.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");

Thay data_int bằng dữ liệu integer của bạn