Hướng dẫn get second javascript - nhận javascript thứ hai

Examples

Get the seconds:

const d = new Date[]; let seconds = d.getSeconds[];
let seconds = d.getSeconds[];

Try it Yourself »

Add zeros and colons to display the time:

function addZero[i] {   if [i < 10] {i = "0" + i}   return i;}
  if [i < 10] {i = "0" + i}
  return i;
}

const d = new Date[]; let h = addZero[d.getHours[]]; let m = addZero[d.getMinutes[]]; let s = addZero[d.getSeconds[]]; let time = h + ":" + m + ":" + s;
let h = addZero[d.getHours[]];
let m = addZero[d.getMinutes[]];
let s = addZero[d.getSeconds[]];
let time = h + ":" + m + ":" + s;

Try it Yourself »

Add zeros and colons to display the time:

function addZero[i] {   if [i < 10] {i = "0" + i}   return i;}

const d = new Date[]; let h = addZero[d.getHours[]]; let m = addZero[d.getMinutes[]]; let s = addZero[d.getSeconds[]]; let time = h + ":" + m + ":" + s;

Definition and Usage

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
9 returns the seconds [0 to 59] of a date.

SyntaxParameters
Return ValueType

Description

A number

The seconds of the date [0 to 59].

Browser Support
Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
9 is an ECMAScript1 [ES1] feature.
ES1 [JavaScript 1997] is fully supported in all browsers:Chrome IE Edge
Firefox Firefox Firefox Firefox Firefox Firefox

369

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.
Learn more.

Làm cách nào để nhận được ngày hoặc/và thời gian hiện tại trong vài giây bằng JavaScript?

Khí

5.1492 Huy hiệu vàng22 Huy hiệu bạc29 Huy hiệu đồng2 gold badges22 silver badges29 bronze badges2 gold badges22 silver badges29 bronze badges

Hỏi ngày 30 tháng 9 năm 2010 lúc 11:52Sep 30, 2010 at 11:52Sep 30, 2010 at 11:52

2

var seconds = new Date[].getTime[] / 1000;

.... sẽ cho bạn những giây kể từ nửa đêm, ngày 1 tháng 1 năm 1970

Tài liệu tham khảo

Đã trả lời ngày 30 tháng 9 năm 2010 lúc 11:56Sep 30, 2010 at 11:56Sep 30, 2010 at 11:56

sje397sje397sje397sje397

40.8k8 Huy hiệu vàng85 Huy hiệu bạc103 Huy hiệu Đồng8 gold badges85 silver badges103 bronze badges8 gold badges85 silver badges103 bronze badges

15

Sử dụng

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
5 là một giải pháp không đầy đủ để có được giây, bởi vì nó tạo ra dấu thời gian với các đơn vị dấu phẩy động.
new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds

Thay vào đó sử dụng:

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.

Ngoài ra, các giá trị không có phao an toàn hơn cho các câu lệnh có điều kiện, bởi vì độ chi tiết bạn có được với phao có thể gây ra kết quả không mong muốn. Ví dụ:

if [1405792936.993 < 1405792937] // true

Cảnh báo: Các nhà khai thác bitwise có thể gây ra các vấn đề khi được sử dụng để thao tác dấu thời gian. Ví dụ,

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
6 cũng có thể được sử dụng để "sàn" giá trị thành vài giây, tuy nhiên mã đó gây ra các vấn đề sau: Bitwise operators can cause issues when used to manipulate timestamps. For example, 6 cũng có thể được sử dụng để "sàn" giá trị thành vài giây, tuy nhiên mã đó gây ra các vấn đề sau: Bitwise operators can cause issues when used to manipulate timestamps. For example,

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
6 can also be used to "floor" the value into seconds, however that code causes the following issues:
  1. Theo mặc định, các số JavaScript là loại nổi loại 64 bit [độ chính xác kép] và các toán tử bitwise ngầm chuyển đổi loại đó thành số nguyên đã ký 32 bit. Có thể cho rằng, loại không nên được chuyển đổi hoàn toàn bởi trình biên dịch và thay vào đó, nhà phát triển nên thực hiện chuyển đổi khi cần thiết.
  2. Thời gian thời gian số nguyên được ký kết được tạo ra bởi toán tử bitwise, gây ra vấn đề năm 2038 như được ghi nhận trong các bình luận.

Đã trả lời ngày 19 tháng 7 năm 2014 lúc 18:42Jul 19, 2014 at 18:42Jul 19, 2014 at 18:42

tim-montaguetim-montaguetim-montaguetim-montague

14.7K4 Huy hiệu vàng57 Huy hiệu bạc45 Huy hiệu Đồng4 gold badges57 silver badges45 bronze badges4 gold badges57 silver badges45 bronze badges

7

Dựa trên nhận xét của bạn, tôi nghĩ rằng bạn đang tìm kiếm một cái gì đó như thế này:

var timeout = new Date[].getTime[] + 15*60*1000; //add 15 minutes;

Sau đó, trong séc của bạn, bạn đang kiểm tra:

if[new Date[].getTime[] > timeout] {
  alert["Session has expired"];
}

Đã trả lời ngày 30 tháng 9 năm 2010 lúc 11:54Sep 30, 2010 at 11:54Sep 30, 2010 at 11:54

Nick Cravernick CraverNick CraverNick Craver

616K134 Huy hiệu vàng1293 Huy hiệu bạc1152 Huy hiệu đồng134 gold badges1293 silver badges1152 bronze badges134 gold badges1293 silver badges1152 bronze badges

0

Để có được số giây từ sử dụng Epoch JavaScript:

date = new Date[];
milliseconds = date.getTime[];
seconds = milliseconds / 1000;

Đã trả lời ngày 30 tháng 9 năm 2010 lúc 11:57Sep 30, 2010 at 11:57Sep 30, 2010 at 11:57

LazaruslazarusLazarusLazarus

40,9K4 Huy hiệu vàng43 Huy hiệu bạc54 Huy hiệu đồng4 gold badges43 silver badges54 bronze badges4 gold badges43 silver badges54 bronze badges

0

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
0
Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
1

jQuery

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
2
Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
1

Jonathan

2.3554 Huy hiệu vàng22 Huy hiệu bạc37 Huy hiệu đồng4 gold badges22 silver badges37 bronze badges4 gold badges22 silver badges37 bronze badges

Đã trả lời ngày 29 tháng 9 năm 2015 lúc 14:09Sep 29, 2015 at 14:09Sep 29, 2015 at 14:09

Tôi sử dụng cái này:

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
1

Không cần tạo đối tượng mới [xem DOC Date.now []]

Đã trả lời ngày 24 tháng 6 năm 2020 lúc 13:39Jun 24, 2020 at 13:39Jun 24, 2020 at 13:39

V. Samborv. SamborV. SamborV. Sambor

11.1k5 Huy hiệu vàng44 Huy hiệu bạc61 Huy hiệu đồng5 gold badges44 silver badges61 bronze badges5 gold badges44 silver badges61 bronze badges

Các giải pháp JavaScript này cung cấp cho bạn một phần nghìn giây hoặc giây kể từ nửa đêm, ngày 1 tháng 1 năm 1970.

Giải pháp IE 9+ [tức là 8 hoặc phiên bản cũ hơn không hỗ trợ điều này.]:

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
2

Để có thêm thông tin về

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
7: //developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/date/now

Giải pháp chung:

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
3

Hãy cẩn thận để sử dụng, nếu bạn không muốn một cái gì đó như trường hợp này.

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
4

Đã trả lời ngày 22 tháng 12 năm 2015 lúc 14:19Dec 22, 2015 at 14:19Dec 22, 2015 at 14:19

К с с кКонстантин ВанКонстантин Ван

10,9k7 Huy hiệu vàng55 Huy hiệu bạc65 Huy hiệu Đồng7 gold badges55 silver badges65 bronze badges7 gold badges55 silver badges65 bronze badges

1

Không cần phải khởi tạo một biến để chứa đối tượng ngày do thực tế là

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
7 là một phương thức tĩnh có nghĩa là có thể truy cập trực tiếp từ hàm tạo của đối tượng API.no need to initialize a variable to contain the Date object due to the fact the
Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
7 is a static method which means that is accessible directly from an API object's constructor.no need to initialize a variable to contain the Date object due to the fact the
Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
7 is a static method which means that is accessible directly from an API object's constructor.

Vì vậy, bạn chỉ có thể làm điều này

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
5

Một cái gì đó vui vẻ

Cập nhật trực tiếp vài giây kể từ ngày 1 tháng 1 năm 1970 00:00:00 UTC

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
6
new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
7

Đã trả lời ngày 24 tháng 3 lúc 9:55Mar 24 at 9:55Mar 24 at 9:55

GassgassGassGass

5.1492 Huy hiệu vàng22 Huy hiệu bạc29 Huy hiệu đồng2 gold badges22 silver badges29 bronze badges2 gold badges22 silver badges29 bronze badges

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
8

Điều này sẽ cung cấp cho bạn một phần nghìn giây từ đầu ngày.

new Date[] / 1000; // 1405792936.933

// Technically, .933 would be in milliseconds
9

Điều này sẽ cho bạn giây.

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
0

Giống như trước đây ngoại trừ sử dụng một toán tử bitwise để tăng số lượng ngày.

Đã trả lời ngày 19 tháng 7 năm 2018 lúc 1:09Jul 19, 2018 at 1:09Jul 19, 2018 at 1:09

Bạn có thể gặp một cách khác để có thời gian trong vài giây/mili giây kể từ ngày 1 tháng 1 năm 1970:

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
1

Nhưng hãy cẩn thận với cách tiếp cận như vậy, vì nó có thể là khó khăn để đọc và hiểu nó.

Đã trả lời ngày 30 tháng 8 năm 2014 lúc 7:34Aug 30, 2014 at 7:34Aug 30, 2014 at 7:34

Tettatettatettatetta

4252 Huy hiệu bạc12 Huy hiệu Đồng2 silver badges12 bronze badges2 silver badges12 bronze badges

1

Cắt ngắn tốt hơn:

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
2

Đã trả lời ngày 4 tháng 9 năm 2015 lúc 12:07Sep 4, 2015 at 12:07Sep 4, 2015 at 12:07

dr.dimitrudr.dimitrudr.dimitrudr.dimitru

2.5751 Huy hiệu vàng27 Huy hiệu bạc36 Huy hiệu đồng1 gold badge27 silver badges36 bronze badges1 gold badge27 silver badges36 bronze badges

Vào một ngày nào đó vào năm 2020, bên trong Chrome 80.0.3987.132, điều này cho

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
9
Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
3

Đã trả lời ngày 18 tháng 3 năm 2020 lúc 12:07Mar 18, 2020 at 12:07Mar 18, 2020 at 12:07

Etorickyetorickyetorickyetoricky

5811 Huy hiệu vàng6 Huy hiệu bạc9 Huy hiệu đồng1 gold badge6 silver badges9 bronze badges1 gold badge6 silver badges9 bronze badges

Để có được tổng số giây của ngày hôm nay trong ngày:

Math.round[Date.now[] / 1000]; // 1405792937

// Or
Math.floor[Date.now[] / 1000]; // 1405792936

// Or
Math.ceil[Date.now[] / 1000]; // 1405792937

// Note: In general, I recommend `Math.round[]`,
//   but there are use cases where
//   `Math.floor[]` and `Math.ceil[]`
//   might be better suited.
4

Tôi đã thêm

if [1405792936.993 < 1405792937] // true
0 trở lại vào trong
if [1405792936.993 < 1405792937] // true
1. Điều này có thể giúp các nhà phát triển khác. :]

Đã trả lời ngày 28 tháng 11 năm 2018 lúc 6:52Nov 28, 2018 at 6:52Nov 28, 2018 at 6:52

Sachin Shahsachin ShahSachin ShahSachin Shah

4.3463 Huy hiệu vàng20 Huy hiệu bạc48 Huy hiệu đồng3 gold badges20 silver badges48 bronze badges3 gold badges20 silver badges48 bronze badges

Nếu bạn chỉ cần cần vài giây trong ba JS, hãy sử dụng một trong các mã dưới dạng trong chức năng sử dụng Window.RequestAnimationFrame []

if [1405792936.993 < 1405792937] // true
2

hoặc

if [1405792936.993 < 1405792937] // true
3
if [1405792936.993 < 1405792937] // true
4
if [1405792936.993 < 1405792937] // true
5
if [1405792936.993 < 1405792937] // true
6

Đã trả lời ngày 5 tháng 11 năm 2021 lúc 3:11Nov 5, 2021 at 3:11Nov 5, 2021 at 3:11

1

Bài Viết Liên Quan

Chủ Đề