JavaScript擷取時間

2021-07-24 19:52:27 字數 1483 閱讀 4357

最近遇到了發票列印當前時間的問題,js自帶的函式就可以獲取當前時間。

可以用 date()

方法獲得當日的日期

:date

物件自動使用當前的日期和時間作為其初始值。

mydate.getyear();        //

獲取當前年份(2位

) mydate.getfullyear();    //獲取完整的年份(4位

,1970-????)

mydate.getmonth();       //獲取當前月份

(0-11,0代表1

月)mydate.getdate();        //獲取當前日

(1-31)

mydate.getday();         //獲取當前星期

x(0-6,0

代表星期天

)mydate.gettime();        //獲取當前時間(從

1970.1.1

開始的毫秒數

)mydate.gethours();       //獲取當前小時數

(0-23)

mydate.getminutes();     //獲取當前分鐘數

(0-59)

mydate.getseconds();     //獲取當前秒數

(0-59)

mydate.getmilliseconds();    //獲取當前毫秒數

(0-999)

mydate.tolocaledatestring();     //獲取當前日期

var mytime=mydate.tolocaletimestring();     //獲取當前時間

mydate.tolocalestring( );        //獲取日期與時間

舉例:

function curenttime();//把個位數的十位置加「0」湊成兩位數,

var currentdate = new date(),

var year = datedigittostring(currentdate.getfullyear()),

month = datedigittostring(currentdate.getmonth() + 1),

date = datedigittostring(currentdate.getdate()),

hour = datedigittostring(currentdate.gethours()),

minute = datedigittostring(currentdate.getminutes()),

second = datedigittostring(currentdate.getseconds()),

now_times = year + '年' + month + '月' + date + '日 ' + hour + ':' + minute + ':' + second;

return now_times;//當前時間

}

mysql 時間擷取 慢日誌按時間擷取

今天處理乙個case 資料庫異常,連線數突增。想著分析一下慢日誌,可是一看慢日誌都好幾g了,而且是短日誌格式,找到那個時間點相對比較難。於是寫了乙個指令碼從慢日誌按時間提取點日誌。指令碼 使用方法 cutlogbytime 用於從慢日誌用擷取乙個時間段的日誌方便分析 cutlogbytime pat...

javascript字串擷取

1.substring 方法 定義和用法 substring 方法用於提取字串中介於兩個指定下標之間的字元。語法stringobject.substring start,stop 引數 描述 start 必需。乙個非負的整數,規定要提取的子串的第乙個字元在 stringobject 中的位置。sto...

javascript字串擷取

substr 方法 返回乙個從指定位置開始的指定長度的子字串。strin ar.substr start length 引數strin ar 必選項。要提取子字串的字串文字或 string 物件。start 必選項。所需的子字串的起始位置。字串中的第乙個字元的索引為 0。length 可選項。在返回...