• JS如何将UTC格式时间转本地格式


    Date.prototype.format = function (format) {
    var o = {
    "M+": this.getMonth() + 1, //month
    "d+": this.getDate(), //day
    "h+": this.getHours(), //hour
    "m+": this.getMinutes(), //minute
    "s+": this.getSeconds(), //second
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
    "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length == 1 ? o[k] :
    ("00" + o[k]).substr(("" + o[k]).length));
    return format;
    }

    var TempDate = new Date();

    TempDate.toLocaleDateString()//2013年9月4日

    TempDate.format("yyyy-MM-dd")//2013-09-04

    以上为将utc格式时间转换为本地正常时间的工具类,以后在遇到时间对象转化时可以直接套用该工具类,实现时间格式的正常显示。

    总结:

    date对象的常用方法:

     Date 对象的方法简介: 

      ·getDate      | 根据本地时间获取当前日期(本月的几号)

      ·getDay       | 根据本地时间获取今天是星期几(0-Sunday,1-Monday...)

      ·getFullYear    | 根据本地时间获取当前年份(四位数字) 

      ·getHours      | 根据本地时间获取当前小时数(24小时制,0-23)

      ·getMilliseconds  | 根据本地时间获取当前毫秒数

      ·getMinutes     | 根据本地时间获取当前分钟数

      ·getMonth      | 根据本地时间获取当前月份(注意从0开始:0-Jan,1-Feb...)

      ·getSeconds     | 根据本地时间获取当前秒数

      ·getTime      | 获取UTC格式的从1970.1.1 0:00以来的毫秒数

      ·getTimezoneOffset | 获取当前时间和UTC格式的偏移值(以分钟为单位)

      ·getUTCDate     | 获取UTC格式的当前日期(本月的几号)

      ·getUTCDay     | 获取UTC格式的今天是星期几(0-Sunday,1-Monday...)

      ·getUTCFullYear   | 获取UTC格式的当前年份(四位数字)

      ·getUTCHours    | 获取UTC格式的当前小时数(24小时制,0-23)

      ·getUTCMilliseconds | 获取UTC格式的当前毫秒数

      ·getUTCMinutes   | 获取UTC格式的当前分钟数

      ·getUTCMonth    | 获取UTC格式的当前月份(注意从0开始:0-Jan,1-Feb...)

      ·getUTCSeconds   | 获取UTC格式的当前秒数

      ·getYear      | 根据本地时间获取当前缩写年份(当前年份减去1900)

      ·setDate      | 设置当前日期(本月的几号)

      ·setFullYear    | 设置当前年份(四位数字)

      ·setHours      | 设置当前小时数(24小时制,0-23)

      ·setMilliseconds  | 设置当前毫秒数

      ·setMinutes     | 设置当前分钟数

      ·setMonth      | 设置当前月份(注意从0开始:0-Jan,1-Feb...)

      ·setSeconds     | 设置当前秒数

      ·setTime      | 设置UTC格式的从1970.1.1 0:00以来的毫秒数

      ·setUTCDate     | 设置UTC格式的当前日期(本月的几号)

      ·setUTCFullYear   | 设置UTC格式的当前年份(四位数字)

      ·setUTCHours    | 设置UTC格式的当前小时数(24小时制,0-23)

      ·setUTCMilliseconds | 设置UTC格式的当前毫秒数

      ·setUTCMinutes   | 设置UTC格式的当前分钟数

      ·setUTCMonth    | 设置UTC格式的当前月份(注意从0开始:0-Jan,1-Feb...)

      ·setUTCSeconds   | 设置UTC格式的当前秒数

      ·setYear      | 设置当前缩写年份(当前年份减去1900)

      ·toString      | 将日期时间值转换成"日期/时间"形式的字符串值

      ·Date.UTC      | 返回指定的UTC格式日期时间的固定时间值 

    date对象使用总结:

  • 相关阅读:
    linux-tar备份
    实验
    华为HCIA-命令及配置
    1、基本知识
    linux 安装samba
    POJ 2676 Sudoku (搜索,Dancing Links)
    HDU 1535 Invitation Cards (最短路,附SLF优化SPFA)
    ZOJ 3625 Geek's Collection (数学公式,注意long double输出格式,附输出格式总结)
    SGU 185 Two shortest (最大流)
    SPOJ PROFIT Maximum Profit (最大闭合权子图,最小割)
  • 原文地址:https://www.cnblogs.com/moonfans/p/3362893.html
Copyright © 2020-2023  润新知