最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于jQuery的時(shí)間戳與日期間的轉(zhuǎn)化

 更新時(shí)間:2019年06月21日 08:33:42   作者:jwensh  
這篇文章主要為大家詳細(xì)介紹了基于jQuery的時(shí)間戳與日期間的轉(zhuǎn)化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了jQuery時(shí)間戳與日期間的轉(zhuǎn)化代碼,供大家參考,具體內(nèi)容如下

背景:

需求如圖:

 直接上代碼,所有的內(nèi)容都在注釋里:

/**
 * 格式化時(shí)間:補(bǔ)0操作
 * */
function supplement(num){
  if(parseInt(num) < 10){
    num = '0'+num;
  }
  return num;
};
 
/**
 * 格式化時(shí)間:拓展jquery的全局變量
 * */
$.extend({
  JTime:{
    //當(dāng)前時(shí)間戳 秒:如果要毫秒就不除以1000
    newTime: function(){
      //本地時(shí)間然后在轉(zhuǎn)為時(shí)間戳,沒(méi)有時(shí)區(qū)區(qū)別 == Date.now()
      return Date.parse(new Date())/1000;
    },
    //日期格式(YY-mm-dd HH:MM:SS)轉(zhuǎn)時(shí)間戳(秒)
    DateToTamp: function(oString) {
      var f = oString.split(' ', 2);
      var d = (f[0] ? f[0] : '').split('-', 3);
      var t = (f[1] ? f[1] : '').split(':', 3);
      //使用Date的構(gòu)造函數(shù),實(shí)力化并解析
      return (new Date(
        parseInt(d[0], 10) || null,
        (parseInt(d[1], 10) || 1) - 1,
        parseInt(d[2], 10) || null,
        parseInt(t[0], 10) || null,
        parseInt(t[1], 10) || null,
        parseInt(t[2], 10) || null
      )).getTime() / 1000;
    },
    //時(shí)間戳(秒)轉(zhuǎn)日期時(shí)間格式(YY-mm-dd [HH:MM:SS]):有條件的轉(zhuǎn)(時(shí)間戳, 是否解析時(shí)間,時(shí)區(qū):中國(guó)=8)
    TampToDate: function(unixTime, isFull, timeZone) {
      //時(shí)區(qū)處理
      if (typeof (timeZone) === 'number'){
        unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
      }
      var time = new Date(unixTime * 1000);
      var ymdhis = "";
      ymdhis += time.getUTCFullYear() + "-";
      ymdhis += (time.getUTCMonth()+1) + "-";
      ymdhis += time.getUTCDate();
      //需要完整的就設(shè)置true
      if (isFull === true){
        ymdhis += " " + time.getUTCHours() + ":";
        ymdhis += time.getUTCMinutes() + ":";
        ymdhis += time.getUTCSeconds();
      }
      return ymdhis;
    },
    //時(shí)間戳(毫秒)轉(zhuǎn)日期時(shí)間格式
    TampToDatetime: function (str) {
      var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth()+1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear +'-'+ supplement(oMonth) +'-'+ supplement(oDay) +' '+ supplement(oHour) +':'+ supplement(oMin) +':'+supplement(oSen); //按格式拼接時(shí)間
      return oTime;
    }
  }
});

原生的api:

interface Date {
  /** Returns a string representation of a date. The format of the string depends on the locale. */
  toString(): string;
  /** Returns a date as a string value. */
  toDateString(): string;
  /** Returns a time as a string value. */
  toTimeString(): string;
  /** Returns a value as a string value appropriate to the host environment's current locale. */
  toLocaleString(): string;
  /** Returns a date as a string value appropriate to the host environment's current locale. */
  toLocaleDateString(): string;
  /** Returns a time as a string value appropriate to the host environment's current locale. */
  toLocaleTimeString(): string;
  /** Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC. */
  valueOf(): number;
  /** Gets the time value in milliseconds. */
  getTime(): number;
  /** Gets the year, using local time. */
  getFullYear(): number;
  /** Gets the year using Universal Coordinated Time (UTC). */
  getUTCFullYear(): number;
  /** Gets the month, using local time. */
  getMonth(): number;
  /** Gets the month of a Date object using Universal Coordinated Time (UTC). */
  getUTCMonth(): number;
  /** Gets the day-of-the-month, using local time. */
  getDate(): number;
  /** Gets the day-of-the-month, using Universal Coordinated Time (UTC). */
  getUTCDate(): number;
  /** Gets the day of the week, using local time. */
  getDay(): number;
  /** Gets the day of the week using Universal Coordinated Time (UTC). */
  getUTCDay(): number;
  /** Gets the hours in a date, using local time. */
  getHours(): number;
  /** Gets the hours value in a Date object using Universal Coordinated Time (UTC). */
  getUTCHours(): number;
  /** Gets the minutes of a Date object, using local time. */
  getMinutes(): number;
  /** Gets the minutes of a Date object using Universal Coordinated Time (UTC). */
  getUTCMinutes(): number;
  /** Gets the seconds of a Date object, using local time. */
  getSeconds(): number;
  /** Gets the seconds of a Date object using Universal Coordinated Time (UTC). */
  getUTCSeconds(): number;
  /** Gets the milliseconds of a Date, using local time. */
  getMilliseconds(): number;
  /** Gets the milliseconds of a Date object using Universal Coordinated Time (UTC). */
  getUTCMilliseconds(): number;
  /** Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). */
  getTimezoneOffset(): number;
  /**
   * Sets the date and time value in the Date object.
   * @param time A numeric value representing the number of elapsed milliseconds since midnight, January 1, 1970 GMT.
   */
  setTime(time: number): number;
  /**
   * Sets the milliseconds value in the Date object using local time.
   * @param ms A numeric value equal to the millisecond value.
   */
  setMilliseconds(ms: number): number;
  /**
   * Sets the milliseconds value in the Date object using Universal Coordinated Time (UTC).
   * @param ms A numeric value equal to the millisecond value.
   */
  setUTCMilliseconds(ms: number): number;
 
  /**
   * Sets the seconds value in the Date object using local time.
   * @param sec A numeric value equal to the seconds value.
   * @param ms A numeric value equal to the milliseconds value.
   */
  setSeconds(sec: number, ms?: number): number;
  /**
   * Sets the seconds value in the Date object using Universal Coordinated Time (UTC).
   * @param sec A numeric value equal to the seconds value.
   * @param ms A numeric value equal to the milliseconds value.
   */
  setUTCSeconds(sec: number, ms?: number): number;
  /**
   * Sets the minutes value in the Date object using local time.
   * @param min A numeric value equal to the minutes value.
   * @param sec A numeric value equal to the seconds value.
   * @param ms A numeric value equal to the milliseconds value.
   */
  setMinutes(min: number, sec?: number, ms?: number): number;
  /**
   * Sets the minutes value in the Date object using Universal Coordinated Time (UTC).
   * @param min A numeric value equal to the minutes value.
   * @param sec A numeric value equal to the seconds value.
   * @param ms A numeric value equal to the milliseconds value.
   */
  setUTCMinutes(min: number, sec?: number, ms?: number): number;
  /**
   * Sets the hour value in the Date object using local time.
   * @param hours A numeric value equal to the hours value.
   * @param min A numeric value equal to the minutes value.
   * @param sec A numeric value equal to the seconds value.
   * @param ms A numeric value equal to the milliseconds value.
   */
  setHours(hours: number, min?: number, sec?: number, ms?: number): number;
  /**
   * Sets the hours value in the Date object using Universal Coordinated Time (UTC).
   * @param hours A numeric value equal to the hours value.
   * @param min A numeric value equal to the minutes value.
   * @param sec A numeric value equal to the seconds value.
   * @param ms A numeric value equal to the milliseconds value.
   */
  setUTCHours(hours: number, min?: number, sec?: number, ms?: number): number;
  /**
   * Sets the numeric day-of-the-month value of the Date object using local time.
   * @param date A numeric value equal to the day of the month.
   */
  setDate(date: number): number;
  /**
   * Sets the numeric day of the month in the Date object using Universal Coordinated Time (UTC).
   * @param date A numeric value equal to the day of the month.
   */
  setUTCDate(date: number): number;
  /**
   * Sets the month value in the Date object using local time.
   * @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.
   * @param date A numeric value representing the day of the month. If this value is not supplied, the value from a call to the getDate method is used.
   */
  setMonth(month: number, date?: number): number;
  /**
   * Sets the month value in the Date object using Universal Coordinated Time (UTC).
   * @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.
   * @param date A numeric value representing the day of the month. If it is not supplied, the value from a call to the getUTCDate method is used.
   */
  setUTCMonth(month: number, date?: number): number;
  /**
   * Sets the year of the Date object using local time.
   * @param year A numeric value for the year.
   * @param month A zero-based numeric value for the month (0 for January, 11 for December). Must be specified if numDate is specified.
   * @param date A numeric value equal for the day of the month.
   */
  setFullYear(year: number, month?: number, date?: number): number;
  /**
   * Sets the year value in the Date object using Universal Coordinated Time (UTC).
   * @param year A numeric value equal to the year.
   * @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively. Must be supplied if numDate is supplied.
   * @param date A numeric value equal to the day of the month.
   */
  setUTCFullYear(year: number, month?: number, date?: number): number;
  /** Returns a date converted to a string using Universal Coordinated Time (UTC). */
  toUTCString(): string;
  /** Returns a date as a string value in ISO format. */
  toISOString(): string;
  /** Used by the JSON.stringify method to enable the transformation of an object's data for JavaScript Object Notation (JSON) serialization. */
  toJSON(key?: any): string;
}
 
interface DateConstructor {
  new(): Date;
  new(value: number): Date;
  new(value: string): Date;
  new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date;
  (): string;
  readonly prototype: Date;
  /**
   * Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.
   * @param s A date string
   */
  parse(s: string): number;
  /**
   * Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.
   * @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.
   * @param month The month as an number between 0 and 11 (January to December).
   * @param date The date as an number between 1 and 31.
   * @param hours Must be supplied if minutes is supplied. An number from 0 to 23 (midnight to 11pm) that specifies the hour.
   * @param minutes Must be supplied if seconds is supplied. An number from 0 to 59 that specifies the minutes.
   * @param seconds Must be supplied if milliseconds is supplied. An number from 0 to 59 that specifies the seconds.
   * @param ms An number from 0 to 999 that specifies the milliseconds.
   */
  UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number;
  now(): number;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js判斷復(fù)選框是否選中的方法示例【基于jQuery】

    js判斷復(fù)選框是否選中的方法示例【基于jQuery】

    這篇文章主要介紹了js判斷復(fù)選框是否選中的方法,結(jié)合實(shí)例形式分析了基于jQuery實(shí)現(xiàn)的復(fù)選框選中判斷相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • jquery鍵盤(pán)事件使用介紹

    jquery鍵盤(pán)事件使用介紹

    jquery鍵盤(pán)事件使用介紹,需要的朋友可以參考下。
    2011-11-11
  • jQuery實(shí)現(xiàn)可兼容IE6的滾動(dòng)監(jiān)聽(tīng)功能

    jQuery實(shí)現(xiàn)可兼容IE6的滾動(dòng)監(jiān)聽(tīng)功能

    這篇文章主要介紹了jQuery實(shí)現(xiàn)可兼容IE6的滾動(dòng)監(jiān)聽(tīng)功能,結(jié)合實(shí)例形式分析了jQuery針對(duì)不同瀏覽器的事件監(jiān)聽(tīng)、響應(yīng)及頁(yè)面屬性動(dòng)態(tài)變換相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-09-09
  • jquery 元素控制(追加元素/追加內(nèi)容)介紹及應(yīng)用

    jquery 元素控制(追加元素/追加內(nèi)容)介紹及應(yīng)用

    一、在元素內(nèi)部/外部追加元素二、在元素的不同位置追加內(nèi)容三、在元素的開(kāi)始位置追加內(nèi)容四、在不同元素的開(kāi)始位置追加內(nèi)容等等,感興趣的朋友可以參考下哈
    2013-04-04
  • jQuery中click事件用法實(shí)例

    jQuery中click事件用法實(shí)例

    這篇文章主要介紹了jQuery中click事件用法,以實(shí)例形式分析了click事件的具體功能及應(yīng)用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • jQuery點(diǎn)擊其他地方時(shí)菜單消失的實(shí)現(xiàn)方法

    jQuery點(diǎn)擊其他地方時(shí)菜單消失的實(shí)現(xiàn)方法

    這篇文章主要介紹了jQuery點(diǎn)擊其他地方時(shí)菜單消失的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了jQuery實(shí)現(xiàn)點(diǎn)擊其他地方讓菜單消失的實(shí)現(xiàn)思路與解決方法,需要的朋友可以參考下
    2016-04-04
  • jQuery操作HTML代碼方法介紹

    jQuery操作HTML代碼方法介紹

    這篇文章介紹了jQuery操作HTML代碼的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • jquery滾動(dòng)加載數(shù)據(jù)的方法

    jquery滾動(dòng)加載數(shù)據(jù)的方法

    這篇文章主要介紹了jquery滾動(dòng)加載數(shù)據(jù)的方法,實(shí)例分析了jQuery動(dòng)態(tài)加載數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • jQuery頁(yè)面加載初始化的3種方法(推薦)

    jQuery頁(yè)面加載初始化的3種方法(推薦)

    下面小編就為大家?guī)?lái)一篇jQuery頁(yè)面加載初始化的幾種方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • jQuery 自定義函數(shù)寫(xiě)法分享

    jQuery 自定義函數(shù)寫(xiě)法分享

    jQuery 自定義函數(shù)寫(xiě)法分享,需要的朋友可以參考下
    2012-03-03

最新評(píng)論

咸丰县| 瑞丽市| 尼玛县| 射阳县| 天水市| 太和县| 盈江县| 苗栗市| 昌邑市| 广元市| 金堂县| 昌都县| 水城县| 梧州市| 林州市| 漳州市| 揭阳市| 渭源县| 缙云县| 石狮市| 聊城市| 张家港市| 宿松县| 嘉峪关市| 班玛县| 遂平县| 玉屏| 个旧市| 荥经县| 宜阳县| 芷江| 屯门区| 钦州市| 阿坝| 胶南市| 大足县| 视频| 榕江县| 宁海县| 陇川县| 独山县|