zoukankan      html  css  js  c++  java
  • js应用中的小细节-时间戳的转换和input输入框有效数字

    1 input输入框内value值保留有效数字,js自带的方法.toFixed(),但是直接使用会报错,因为不论输入框内输入汉字、字母还是数字,类型都是string。解决的办法是将其转换为number类型。parseInt()、parseFloat()就排上用场了,经过转换后,再使用.toFixed(),结果就是我们想要的;

    1 var input = document.getElementById('#input');
    2 console.log(parseFloat(input.value).toFixed(2));//保留2位有效数字
    3 //toFixed(),括号内数字是想要保留的有效数字位数,

    2 时间戳与标准时间的相互转换

    1 //时间戳=>时间
    2 function getLocalTime(nS) {
    3   var dat = newDate(parseInt(nS)*1000).toLocaleString().replace(/:d{1,2}$/, ' ');
    4     var strs = dat.substr(0, 9);
    5     return strs.split('/');
    6 }
    7 console.log(getLocalTime(1366666666))//返回的是数组["2013", "4", "23"]
    //时间=>时间戳
    function timetamp(y, m, d) {
        return(new Date(y, m, d, ).getTime() / 1000);
    }
    console.log(timetamp(2017, 8, 8))//输出是10位数的值 1504800000
     1 //时间戳转换为时间格式 yyyy-MM-dd hh:mm
     2 function getLocalTime(nS) {
     3     var dat = new Date(parseInt(nS) * 1000).format("yyyy-MM-dd hh:mm");
     4     return dat;
     5 }
     6 var time = getLocalTime(1500666666);
     7 Date.prototype.format = function(format) {
     8     var date = {
     9         "M+": this.getMonth() + 1,
    10         "d+": this.getDate(),
    11         "h+": this.getHours(),
    12         "m+": this.getMinutes(),
    13         "s+": this.getSeconds(),
    14         "q+": Math.floor((this.getMonth() + 3) / 3),
    15         "S+": this.getMilliseconds()
    16     };
    17     if(/(y+)/i.test(format)) {
    18         format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    19     }
    20     for(var k in date) {
    21         if(new RegExp("(" + k + ")").test(format)) {
    22             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
    23                 date[k] : ("00" + date[k]).substr(("" + date[k]).length));
    24         }
    25     }
    26     return format;
    27 }
  • 相关阅读:
    java注解实例-反射生成sql
    应用服务器集群的伸缩性设计
    高可用的服务
    应用服务器集群Session管理
    应用服务器性能优化 (存储性能优化)
    应用服务器性能优化 (代码优化)
    应用服务器性能优化 (使用集群)
    应用服务器性能优化 (异步操作)
    应用服务器性能优化 (分布式缓存)
    Web前端性能优化(CDN加速及反向代理)
  • 原文地址:https://www.cnblogs.com/web-wjg/p/7496418.html
Copyright © 2011-2022 走看看