1. 程式人生 > >微信小程式js日期格式轉化及加減

微信小程式js日期格式轉化及加減

專案中需要用到日期的格式轉化及相關的加減,根據需要的情況,整理了部分方法。並列出date的構造方法及方法以作記錄。

一、以下是根據小程式demo中util.js檔案修改的

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return
[year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } //獲取當前日期,以“/”連線 const formatDate = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return [year, month, day].map(formatNumber).join('/'
) } //獲取當前日期,以“-”連線 const formatDateByH = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return [year, month, day].map(formatNumber).join('-') } //將string格式日期轉換為“/”連線只包含月日的日期 const formatDateToSimple = data => { var date = new
Date(Date.parse(data)); const month = date.getMonth() + 1 const day = date.getDate() return [month, day].join('/') } //獲取string格式日期的星期 const formatDateToWeek = data => { var date = new Date(Date.parse(data)); const month = date.getDay(); var weekDay ; switch (month){ case 0: weekDay = '週日'; break; case 1: weekDay = '週一'; break; case 2: weekDay = '週二'; break; case 3: weekDay = '週三'; break; case 4: weekDay = '週四'; break; case 5: weekDay = '週五'; break; case 6: weekDay = '週六'; break } return weekDay; } //日期的加減 const addDay = data => { //下面的不是時間戳,是時間戳*1000 var timestamp = Date.parse(new Date()); var newTimestamp = timestamp + data * 24 * 60 * 60 * 1000; var date = new Date(newTimestamp); const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); return [year, month, day].map(formatNumber).join('-'); } //月份的加減 const addMonth = num => { if(typeof num == "string"){ num = parseInt(num); } var date = new Date(); const curYear = date.getFullYear(); const curMonth = date.getMonth() + 1; const curDay = date.getDate(); let month = (curMonth + num - 1) % 12; let year = curYear + (curMonth + num - month)/12; let days = curDay; date = new Date(year, month, days); year = date.getFullYear(); month = date.getMonth() + 1; const day = date.getDate(); return [year, month, day].map(formatNumber).join('-') } //月份第幾天增加後獲取月份的第幾天 const getDayByAddDay = data => { //下面的不是時間戳,是時間戳*1000 var timestamp = Date.parse(new Date()); var newTimestamp = timestamp + data * 24 * 60 * 60 * 1000; var date = new Date(newTimestamp); return date.getDate(); } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } module.exports = { formatTime: formatTime, formatDate: formatDate, formatDateByH: formatDateByH, addDay: addDay, addMonth: addMonth, getDayByAddDay: getDayByAddDay, formatDateToSimple: formatDateToSimple, formatDateToWeek: formatDateToWeek, }

二、主要用到函式Date。
1、建構函式

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;
}

2、Date的方法

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;
}

相關推薦

程式js日期格式轉化

專案中需要用到日期的格式轉化及相關的加減,根據需要的情況,整理了部分方法。並列出date的構造方法及方法以作記錄。 一、以下是根據小程式demo中util.js檔案修改的 const formatTime = date => { const yea

程式—修改日期

最近專案開發在做微信小程式專案,記錄一下開發過程中用到的一些小東西,實現的是一個點選增加減小日期的小東西, 貼一下效果圖 大概就是這個效果,可以點選減小日期,點選增大日期,也可以直接選擇日期,相應的判斷日期大小給出相應的提示。 貼一下程式碼 <!-- wxml -->

程式js陣列倒序reverse

微信小程式js陣列倒序reverse 普通.js用法 var a = [1,2,3]; //建立陣列· alert(a.reverse()); //顛倒順序並輸出 輸出結果321 微信小程式.js用法 var a = [1,2,3]; //建立陣列· console.

程式JS實現監控

瀏覽器web端的SDK資料埋點上報,其實原理大同小異:通過劫持原始方法,獲取需要上報的資料,最後再執行原始方法,這樣就能實現無痕埋點。 我們在開發的時候,傳送ajax一般用的都是封裝好的庫,例如jQuery,Axios等,然而這些庫,底層仍然用的是瀏覽器原生的

程式訂單頁面格式

我在開發一個點餐小程式時,遇到了一個問題,使用者通過小程式進入商家,選中自己想下單的菜,如果同一個選了兩份,那麼訂單會顯示菜名乘以2,如果在餐廳吃的情況下是沒有問題的,如果要有一份帶走,那這時候出現的問題就是選中打包的地方只有一個,無論你要幾份,打包只有一份,所

程式js頁面引用/utils函式引用

在小程式中,主要用到程式碼函式的複用,並且使得自己定義的js檔案像utils一樣可以被其他檔案引用,以下兩個方面: 1.被引用:在utils被呼叫的js檔案中,面向物件的方式模型輸出:  module.exports={要呼叫的函式名稱:要呼叫的函式名稱 }; 2.引用端

[程式]js動態改變陣列物件列表中的樣式

 有問題可以掃碼加我微信,有償解決問題。承接小程式開發。 微信小程式開發交流qq群   173683895  、 526474645 ; 正文: 這裡我用微信小程式商城開發中選擇商品規格選擇做示例: 先把效果圖讓大家看看,  預設情況下是這樣的 當點選了規格11

程式 js詳解

data - 實現頁面的初始化資料 onLoad - 是生命週期函式,用來監聽頁面載入,一個頁面只會呼叫一次,它的引數可以獲取wx.navigateTo和wx.redirectTo及< navigator/>中的query. onReady - 同樣是生命週

程式 js動態給前臺元素繫結事件

//js動態給前臺繫結事件 var myList = ["items1","items2","items3"] pageObject = { data:{ actionSheet

mpvue 程式 picker 日期元件,使用方法

上圖不說話 template 部分 <div class="index_section"> <picker mode="date" :value="startDate" :start="pickerStart" end="22

程式JS匯出和匯入

1. 匯出   1.1 方法和變數匯出(寫在被匯出方法和變數的js檔案) module.exports = { variable: value, method : methodName }1.

程式多媒體檔案上傳下載(springboot框架中)

/** * 微信檔案上傳介面 * @param file 待上傳檔案的完整路徑 */ public FileUploadOrDownload uploadTempMedia(String file) { /

程式之店鋪評分元件vue中用svg實現的評分顯示元件

在微信小程式中,有遇到要展示店鋪評分,或者是訂單完成後對商品進行評價,用到了星星展示,查了下,在微信中無法使用svg實現圖片,微信中只能將svg圖片轉成base64來顯示,所以是在vue中使用的svg來實現評分 1.效果圖 微信中的可以點選及顯示,但是,顯示的話,在4.2分,

程式前端原始碼詳解例項分析

微信小程式前端原始碼邏輯和工作流 看完微信小程式的前端程式碼真的讓我熱血沸騰啊,程式碼邏輯和設計一目瞭然,沒有多餘的東西,真的是大道至簡。 廢話不多說,直接分析前端程式碼。個人觀點,難免有疏漏,僅供參考。 檔案基本結構: 先看入口app.js,app(obj)註冊一個小程式。接受一個 obje

程式掃碼的程式碼獲取二維碼的url地址

<view bindtap="scanCode">點選掃碼</view> scanCode: function (options) { var that = this;

程式:一些執行細節針對性的優化策略

為什麼要做效能優化? 一切效能優化都是為了體驗優化 1. 使用小程式時,是否會經常遇到如下問題? 開啟是一直白屏 開啟是loading態,轉好幾圈 我的頁面點了怎麼跳轉這麼慢? 我的列表怎麼越滑越卡? 2. 我們優化的方向有哪些? 啟

程式開發之獲取openid使用者資訊

1. 獲取openid 1.1 獲取code 呼叫介面獲取登入憑證(code)進而換取使用者登入態資訊,包括使用者的唯一標識(openid) 及本次登入的會話金鑰(session_key)。使用者資料的加解密通訊需要依賴會話金鑰完成。 wx.login({ //獲

程式開發(6) SSL證書HTTPS伺服器

1. 域名 在萬網購買,略 2. 雲伺服器 阿里雲購買,略 3. 安裝lnmp 使用lnmp.org程式,略 4. 申請證書 阿里雲-管理控制檯-安全(雲盾)-證書服務-購買證書證書型別: 免費型DV SSL選擇品牌: Symantec 購買成功後,繫結域名,配置DNS解析

程式不支援window物件Navigator物件Base64轉碼和解碼問題

本程式碼來源網址:http://www.xuebuyuan.com/zh-hant/1810251.htmlvar base64hash = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';// btoa method//b

程式開發中常見問題解決方法

本文章總結小程式開發中常見的錯誤問題。希望能幫助初學者少走彎路,避免類似的錯誤。 1:出現“指令碼錯誤或者未正確呼叫Page()”的錯誤提示。 解決方法:出現這個錯誤的原因通常是因為index.