使用moment格式化日期

2021-10-01 21:28:36 字數 2948 閱讀 7443

格式化日期

moment().format('yyyy-mm-dd hh:mm:ss'); //2014-09-24 23:36:09 

今天是星期幾:

moment().format('d'); //3 

轉換當前時間的unix時間戳:

moment().format('x');       

7天前的日期:

moment().subtract('days',7).format('yyyy年mm月dd日'); //2023年10月01日

7天後的日期:

moment().add('days',7).format('yyyy年mm月dd日'); //2023年10月01日

moment().subtract('hours',9).format('hh:mm:ss'); 

moment().add('hours',9).format('hh:mm:ss');

建立

moment() // 當前時間

moment("1995-12-25") // 1995-12-25

moment("12-25-1995", "mm-dd-yyyy") // 1995-12-25

moment()

moment(date.now() - 24 * 60 * 60 * 1000) // 昨天

moment(new date(2011, 9, 16)) // 2011-10-16

格式化

moment().format('yyyy年mm月dd日 hh:mm:ss') // 2023年11月11日 22:05:19

moment().format('hh:m:ss') // 10:5:19

moment().format('[yyyy]') // "yyyy"。 裡的會原樣輸出。

轉化成 date 物件

moment().todate()

獲取/設定時間資訊

moment().second() //獲得 秒

moment().second(number) //設定 秒。0 到 59

moment().minute() //獲得 分

moment().minute(number) //設定 分。0 到 59

類似的用法

moment().hour() // 小時

moment().date() // 乙個月裡的第幾天

moment().day() // 星期幾

moment().dayofyear() // 一年裡的第幾天

moment().week() // 一年裡的第幾周

moment().month() // 第幾個月

moment().quarter() // 一年裡的第幾個季度

moment().year() // 年

moment().daysinmonth() // 當前月有多少天

//操作

moment().add(7, 'days') // 之後的第7天。第2個引數還可以是 'months', 'years' 等。注意是複數。

moment().add(7, 'd')// 與上面一行**的執行結果一樣。

moment().subtract(1, 'months') // 上個月

moment().startof('week') // 這週的第一天

moment().startof('hour') // 等效與 moment().minutes(0).seconds(0).milliseconds(0)。

// 還支援 'year','month' 等

moment().endof('week')

// 早於

moment('2010-10-20').isbefore('2010-10-21') // true

moment('2010-10-20').isbefore('2010-12-31', 'year') // false

moment('2010-10-20').isbefore('2011-01-01', 'year') // true

// 是否相等

moment('2010-10-20').issame('2010-10-20') // true

moment('2010-10-20').issame('2009-12-31', 'year')  // false

moment('2010-10-20').issame('2010-01-01', 'year')  // true

// 晚於

moment('2010-10-20').isafter('2010-10-19') // true

moment('2010-10-20').isafter('2010-01-01', 'year') // false

moment('2010-10-20').isafter('2009-12-31', 'year') // true

// 是否在時間範圍內

moment('2010-10-20').isbetween('2010-10-19', '2010-10-25') // true

moment('2010-10-20').isbetween('2010-01-01', '2012-01-01', 'year') // false

moment('2010-10-20').isbetween('2009-12-31', '2012-01-01', 'year') // true

moment().isleapyear()

時間日期格式化 moment庫的基本使用

注意 在時間格式的傳輸過程中,我們為了能使時間在每乙個地區都能準確獲取的,一般存入資料庫的都是,utf8 或者 是時間戳的形式,因為時間戳和utf8的是乙個標準,不會因為地區而異而改變,如下圖 使用方式 npm install moment,不管是瀏覽器還是nodejs服務端,這個庫都可以使用 co...

格式化日期

格式化日期 輸出形式取決於使用者計算機的文化設定。using system using system.globalization public class mainclass string date for int i 0 i format.length i d 07 11 2004 輸出 d su...

格式化日期

function dateformat datestr,types dim datestring if isdate datestr false then datestring end if select case types case 1 datestring year datestr month...