changyuqing

现在vue或者react 一般都引入的有moment.js,官网:http://momentjs.cn/ 

常用的几个记录下:

中文显示设置:在入口文件app.vue文件引入

  import zh_CN from \'ant-design-vue/lib/locale-provider/zh_CN\';
  import moment from \'moment\';
  import \'moment/locale/zh-cn\';
  moment.locale(\'zh-cn\');
引用:
import moment from \'moment\';
 methods: {
    moment, //引入方法
}
使用:
 moment([]).format(\'YYYY-MM-DD HH:mm:ss\');//获取当前时间并转换格式
 moment([]).format(\'LLLL\');// 2021年12月6日星期一上午11点15分
 

~~~~~~~~~~~~~~~~~~~~~~~~~~~2021/12/06分界线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

根据时间(2020-07-20 10:23:21)转换成秒,可用于两个时间的比较

特定日期转换成毫秒

let time = new Date("2020-07-20 10:23:21").getTime(); //1595211801000

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

当前时间的转换成毫秒

获取时间毫秒 var time = Date.now(); //1638760198019

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

字符串转换成日期格式:

方法一格式转换:\'20130505\'.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3");   结果:  "2013-05-05"  ;

方法二格式转换:const str2 = \'20190912\';
const regex2 = /(\d{4})(\d{2})(\d{2})/g;

function replacer(match, p1, p2, p3, offset, string) {
return [p1, p2, p3].join(\' - \');
}

str2.replaceAll(regex2, replacer);//\'2019 - 09 - 12\'

toLocaleString获取的是本地时间(当前电脑的时间)

new Date(+new Date()).toLocaleString()

"2020/1/8 下午6:36:08"

toISOString获取的是国际时间,和北京时间8小时时差。

new Date(+new Date()+8*3600*1000).toISOString()

"2020-01-08T10:36:02.157Z"

方法1:

function formate(){
                var current = new Date(+new Date()+8*3600*1000).toISOString();
                var day = current.split("T")[0];
                var time = current.split("T")[1].split(".")[0];
                return day+" "+time;
            }
            console.log(formate());

方法2:别人总结的

Date.prototype.format = function (format) {
               var args = {
                   "M+": this.getMonth() + 1,
                   "d+": this.getDate(),
                   "h+": this.getHours(),
                   "m+": this.getMinutes(),
                   "s+": this.getSeconds(),
                   "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
                   "S": this.getMilliseconds()
               };
               if (/(y+)/.test(format))
                   format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
               for (var i in args) {
                   var n = args[i];
                   if (new RegExp("(" + i + ")").test(format))
                       format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
               }
               return format;
           };
            alert(new Date().format("yyyy-MM-dd hh:mm:ss"));

方法3:

function getNowFormatDate() {
                var date = new Date();
                var seperator1 = "-";
                var seperator2 = ":";
                var month = date.getMonth() + 1;
                var strDate = date.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                        + " " + date.getHours() + seperator2 + date.getMinutes()
                        + seperator2 + date.getSeconds();
                return currentdate;
            }
            console.log(getNowFormatDate());

原博地址:https://blog.csdn.net/weixin_30845171/article/details/96541586

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2022-01-01
  • 2022-01-23
  • 2021-12-22
  • 2021-11-27
  • 2021-11-27
猜你喜欢
  • 2022-01-29
  • 2022-01-01
  • 2021-11-27
  • 2021-11-27
  • 2021-11-27
  • 2022-01-01
  • 2022-01-01
相关资源
相似解决方案