博客
关于我
JS获取当前时间
阅读量:66 次
发布时间:2019-02-26

本文共 490 字,大约阅读时间需要 1 分钟。

以下是优化后的内容:

创建一个新的日期对象,通过 new Date() 获取当前时间。接下来,分别提取年、月、日、时、分等信息,并按照特定格式进行处理。

首先,获取当前年份:var year = now.getFullYear();。然后,获取当前月份,并注意月份可能为单数字,需在前面补零:var month = now.getMonth() + 1;(月份从1到12)。

接着,获取当前日期:var day = now.getDate();,同样需要处理单数字情况。

对于时间部分,获取小时和分钟:var hh = now.getHours();var mm = now.getMinutes();。其中小时和分钟也需要进行格式处理。

按照ISO 8601标准,首先拼接年-月-日,格式为四位年份、两位月份(前补零)、两位日期(前补零)。然后,拼接时间部分,格式为两位小时(前补零)、冒号、两位分钟(前补零)。

最后,将所有信息拼接成完整的时间字符串,并通过 alert(clock); 显示结果。

整个过程通过简单的条件判断和字符串拼接实现,确保输出格式符合预期要求。

转载地址:http://fbmz.baihongyu.com/

你可能感兴趣的文章
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用操作---npm工作笔记003
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm设置源地址,npm官方地址
查看>>
npm设置镜像如淘宝:http://npm.taobao.org/
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
NPM酷库052:sax,按流解析XML
查看>>
npm错误 gyp错误 vs版本不对 msvs_version不兼容
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
npm,yarn,cnpm 的区别
查看>>