您的当前位置:首页>全部文章>文章详情

【JavaScript】JS获取时间并格式化

CrazyPanda发表于:2024-08-14 15:05:11浏览:301次TAG:

方法一:使用JavaScript的Date对象和相关方法来获取时间并格式化

var now = new Date();
 
// 格式化日期
var year = now.getFullYear();
var month = (now.getMonth() + 1 < 10 ? "0" : "") + (now.getMonth() + 1);
var day = (now.getDate() < 10 ? "0" : "") + now.getDate();
 
// 格式化时间
var hours = (now.getHours() < 10 ? "0" : "") + now.getHours();
var minutes = (now.getMinutes() < 10 ? "0" : "") + now.getMinutes();
var seconds = (now.getSeconds() < 10 ? "0" : "") + now.getSeconds();
 
// 输出格式化后的时间
var formattedTime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
console.log(formattedTime);

方法二:使用第三方库moment.js来获取时间并格式化

var now = moment();
 
// 格式化日期
var formattedDate = now.format("YYYY-MM-DD");
 
// 格式化时间
var formattedTime = now.format("HH:mm:ss");
 
// 输出格式化后的时间
var formattedDateTime = formattedDate + " " + formattedTime;
console.log(formattedDateTime);

方法三:使用Intl对象来获取时间并格式化(需浏览器支持)

var now = new Date();
 
// 格式化日期
var options = { year: 'numeric', month: '2-digit', day: '2-digit' };
var dateFormatter = new Intl.DateTimeFormat('en-US', options);
var formattedDate = dateFormatter.format(now);
 
// 格式化时间
var timeOptions = { hour: '2-digit', minute: '2-digit', second: '2-digit' };
var timeFormatter = new Intl.DateTimeFormat('en-US', timeOptions);
var formattedTime = timeFormatter.format(now);
 
// 输出格式化后的时间
var formattedDateTime = formattedDate + " " + formattedTime;
console.log(formattedDateTime);


猜你喜欢

【JavaScript】JS防抖动方法
防抖动是指在事件被触发时,为了减少因为快速连续操作导致的大量事件触发,通常会设置一个延时,当事件被触发后,等待一段时间,如果在这段时间内没有新的相同事件被触发,则执行该事件的操作。在JavaScript中,防抖动通常可以通过设置定时器来实现。当事件触发时,如果已经有一个定时器在等待,则清除它并重新设置一个新的定时器。只有当定时器到期而事件也没有被触发时,事件绑定的处理函数才会执行。function&nbsp;debounce(fn,&nbsp;wait)&nbsp;{ &nbsp;&nbsp;l
发表于:2024-04-24 浏览:278 TAG:
【JavaScript】JS获取时间并格式化
方法一:使用JavaScript的Date对象和相关方法来获取时间并格式化var&nbsp;now&nbsp;=&nbsp;new&nbsp;Date(); &nbsp; //&nbsp;格式化日期 var&nbsp;year&nbsp;=&nbsp;now.getFullYear(); var&nbsp;month&nbsp;=&nbsp;(now.getMonth()&nbsp;+&nbsp;1&nbsp;&lt;&nbsp;10&nbsp;?&nbsp;&quot;0&quot;&amp;nbs
发表于:2024-08-14 浏览:302 TAG:
【JavaScript】字母大小写转换的JS方法
1. toUpperCase() 方法:将字符串中的所有字母转换为大写。例如,`"hello world".toUpperCase()` 将返回 `"HELLO WORLD"`。
发表于:2024-11-16 浏览:232 TAG: #javascript
【JavaScript】JS时间和时间戳互转
在JavaScript中,获取当前时间的时间戳(秒值)可以使用 Date.now() 方法,而将时间戳转换为日期格式可以使用 Date 对象。获取当前时间戳(秒值):let&nbsp;timestampSeconds&nbsp;=&nbsp;Date.now()&nbsp;/&nbsp;1000; console.log(timestampSeconds);&nbsp;//&nbsp;输出的是以秒为单位的时间戳将时间戳转换为日期:let&nbsp;timestampSeconds&nbsp;=&amp;
发表于:2024-08-14 浏览:264 TAG:
【JavaScript】如何使用 layui 实现自适应
layui 是一个轻量级的现代前端框架,它提供了丰富的 UI 组件和强大的响应式功能。要使用 layui 实现自适应布局,可以按照以下步骤进行:1. 引用 layui 框架复制以下代码并将其粘贴到你的 HTML 页面中:&lt;script&nbsp;src=&quot;path/to/layui.js&quot;&gt;&lt;/script&gt;2. 定义自适应布局的容器创建用于放置自适应内容的容器,并为其设置 layui-container 类:&lt;div&nbsp;class=&amp;q
发表于:2024-08-08 浏览:241 TAG:
【JavaScript】javascript中doucument对象的属性和方法有哪些
doucument对象的属性和方法有:body、cookie、domain、lastmodified、referrer、title、close()、open()、write()、getelementbyid()、normalize()等等。本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。Document 对象每个载入浏览器的 HTML 文档都会成为 Document 对象。Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。
发表于:2024-04-14 浏览:287 TAG:
【JavaScript】JS生成二维码-qrcode.js
二维码又称QRCode,是一个近几年来移动设备上很流行的一种编码方式它比传统的一维码(条形码)能存更多的信息,也能表示更多的数据类型。按照一定规律排列组成的几何图形构成,它巧妙地利用构成计算机内部逻辑...
发表于:2024-10-29 浏览:138 TAG: #二维码 #javascript
【JavaScript】纯CSS蜂巢式图片画廊效果
这是一款纯CSS蜂巢式图片画廊效果。该CSS蜂巢式图片画廊通过CSS网格布局,将图片以蜂巢的六边形进行布局,非常炫酷。
发表于:2024-11-06 浏览:245 TAG: #CSS #图像
【JavaScript】JS四舍五入保留两位小数(一)
1 、tofixed方法toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。但是其四舍五入的规则与数学中的规则不同,使用的是银行家舍入规则。**银行家舍入:所谓银行家舍入法,其实质是一种四舍六入五取偶(又称四舍六入五留双)法。**具体规则如下:简单来说就是:四舍六入五考虑,五后非零就进一,五后为零看奇偶,五前为偶应舍去,五前为奇要进一。如下:&gt;&nbsp;(3.61).toFixed(1)&nbsp;&nbsp;&nbsp;&nbsp;//四舍 &#39;3.6&amp;#3
发表于:2024-03-12 浏览:322 TAG:
【Javascript】CSS3和js超酷iPhone样式科学计算器插件
CalcSS3是一款非常酷的CSS3和js模仿iPhone样式科学计算器插件。该计算器插件没有使用图片,纯CSS制作。该计算器是科学型的,可以处理乘方、开方、指数、对数等复杂的数学运算。
发表于:2024-11-06 浏览:246 TAG: #javascript