js獲取當前時間的年月日時分秒以及時間的格式化
1.獲取當前時間
var myDate = new Date();
2.獲取時間中的年月日時分秒
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( ); // 獲取日期與時間
3.時間的格式化
// 對Date的擴充套件,將 Date 轉化為指定格式的String
// 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個佔位符,
// 年(y)可以用 1-4 個佔位符,毫秒(S)只能用 1 個佔位符(是 1-3 位的數字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { // author: meizz
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), // 日
"h+": this.getHours(), // 小時
"m+": this.getMinutes(), // 分
"s+": this.getSeconds(), // 秒
"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
"S": this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
呼叫:
var time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
相關推薦
js獲取當前時間的年月日時分秒以及時間的格式化
1.獲取當前時間 var myDate = new Date(); 2.獲取時間中的年月日時分秒 myDate.getYear(); // 獲取當前年份(2位) myDat
JS實現頁面以年月日時分秒展示時間
span Oday ont jdb .get () type || 類型 如果想要展示時分秒,在存儲時間時數據庫字段應該是datetime類型,mysql映射文件中應該是jdbcType="TIMESTAMP"類型 以下為JS代碼 function getMy
JS獲取當前時間年月日時分秒
JS獲取當前時間的年月日時分秒,按照格式輸出 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> &
在JS中如何獲得當前年月日時分秒,時間戳,以及具體的時間分類(最全)
在Js中如何獲取當前時間的年月日時分秒,形式如:2017-06-04 13:30:27 獲取的方式如下: function getNowFormatDate() { var date = n
獲取當前時間年月日時分秒
Date.prototype.Format = function (fmt) { // author: meizz var o = { "M+": this.getMonth() + 1, // 月份  
ios獲取當前時間年月日時分秒
//獲取當前時間 NSDate *now = [NSDate date]; NSLog(@”now date is: %@”, now); NSCalendar *calendar = [NSCalendar currentCale
js如何獲得系統時間年月日時分秒
targe onload 調用方法 space m3u baidu function www. pyw javascript 自帶有個對象(構造函數),Date().下面是代碼: 回答一: var now = new Date(); var nowTime = now.t
Android動態顯示當前年月日時分秒系統時間
在佈局檔案中放一個TextView用來顯示時間,如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://sche
c#.net 獲取時間年月日時分秒格式
//獲取日期+時間 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 2008-9-4 20:12:12 //獲取日期 DateTime.Now.T
cocos2d-x 獲取系統時間 年月日時分秒還有毫秒
1秒(s) = 1000 毫秒(ms) = 1,000,000 微秒(μs) = 1,000,000,000 納秒(ns) = 1,000,000,000,000 皮秒(ps) 本函式主要實現獲取系統年月日時分秒還有毫秒。 對網上獲取系統毫秒時,本人覺得寫的有點問題(1秒
Sql 獲取當前日期沒有時分秒
2-2 時分秒 date body () convert get 8.0 OS select convert(varchar(10),getdate(),120) 輸出格式:2008-02-27 00:25:13 SELECT CONVERT(char(19), g
JS獲取當前完整的url地址以及引數的方法
javascript 獲取當前 URL 引數的兩種方法: //返回的是字串形式的引數,例如:class_id=3&id=2& function getUrlArgStr(){ var q=location.search.substr(1);
C/C++獲取當前時刻的時分秒
看程式碼: #include <iostream> using namespace std; int main() { time_t currentTime;
js中獲取當前年月日時分秒
function getNowDate() { var date = new Date(); this.year = date.getFullYear(); this.month = date.getMonth() + 1; this.dat
js獲取當前 年月日-時分秒
function getNowFormatDate() { var date = new Date(); var seperator1 = "-"
Java獲取當前時間的年月日時分秒方法
package com.ob; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
js獲取毫秒值以及轉換成年月日時分秒等
時間日期物件 建立方式一 var date=new Date(); 建立方式二 相容性最強 推薦使用 var date1=new Date("2017/08-2
JAVA獲取當前時間和將已有的long型別時間轉換為年月日時分秒格式
程式碼如下: public class DateUtil { /** * 根據格式獲取當前格式化時間 * @param format 格式化方式,基礎格式為yyyy-MM-dd HH:mm:ss * @return 當前時間 */ publ
C/C++程式碼獲取當前時間的:年月日時分秒
直接上菜: #include <iostream> #include <string> using namespace std; static string
年月日時分秒轉化為秒 和獲取當前時間
//伺服器返回的時間 和當前的時間@SuppressLint("SimpleDateFormat")public static int TimeMillis(String args) throws ParseException{SimpleDateFormat sdf =