2011-05-18 136 views

回答

523
("0" + this.getDate()).slice(-2) 

的日期,以及類似:

("0" + (this.getMonth() + 1)).slice(-2) 

月。

+64

很酷,但:'功能ADDZ( n){return n <10? '0'+ n:''+ n;}'有點泛泛。 – RobG 2011-05-18 06:19:58

+0

謝謝薩爾曼和雨果 – srini 2011-05-18 09:14:52

+5

切片很聰明,但它比一個簡單的比較要慢得多:http://jsperf.com/slice-vs-comparison – dak 2012-05-16 03:25:36

28

舉例月:

function getMonth(date) { 
    var month = date.getMonth() + 1; 
    return month < 10 ? '0' + month : '' + month; // ('' + month) for string result 
} 

您還可以使用這樣的功能擴展Date對象:

Date.prototype.getMonthFormatted = function() { 
    var month = this.getMonth() + 1; 
    return month < 10 ? '0' + month : '' + month; // ('' + month) for string result 
} 
+4

請注意,getMonth返回0到11之間的數字,而不是1和12. – 2011-05-18 06:11:31

+4

這會返回不一致的結果。對於十一月和十二月,它返回一個字符串,在其他幾個月它返回一個數字。 – 2011-05-18 08:59:09

+0

我更新了實現Salman的代碼警告getMonth是基於零而不是1.然後添加引號以確保總是返回一個字符串。 – 2015-05-18 18:53:12

5
function monthFormated() { 
    var date = new Date(), 
     month = date.getMonth(); 
    return month+1 < 10 ? ("0" + month) : month; 
} 
2

這是我的解決方案:

function leadingZero(value) { 
    if (value < 10) { 
    return "0" + value.toString(); 
    } 
    return value.toString(); 
} 

var targetDate = new Date(); 
targetDate.setDate(targetDate.getDate()); 
var dd = targetDate.getDate(); 
var mm = targetDate.getMonth() + 1; 
var yyyy = targetDate.getFullYear(); 
var dateCurrent = leadingZero(mm) + "/" + leadingZero(dd) + "/" + yyyy; 
7

以下是習慣於使用三元運算符轉換DB2的日期格式 即YYYY-MM-DD

var currentDate = new Date(); 
var twoDigitMonth=((currentDate.getMonth()+1)>=10)? (currentDate.getMonth()+1) : '0' + (currentDate.getMonth()+1); 
var twoDigitDate=((currentDate.getDate())>=10)? (currentDate.getDate()) : '0' + (currentDate.getDate()); 
var createdDateTo = currentDate.getFullYear() + "-" + twoDigitMonth + "-" + twoDigitDate; 
alert(createdDateTo); 
2

不是一個答案,但這裏是我得到的日期格式我需要一個變量

function setDateZero(date){ 
    return date < 10 ? '0' + date : date; 
} 

var curr_date = ev.date.getDate(); 
var curr_month = ev.date.getMonth() + 1; 
var curr_year = ev.date.getFullYear(); 
var thisDate = curr_year+"-"+setDateZero(curr_month)+"-"+setDateZero(curr_date); 

希望這有助於!

3
function monthFormated(date) { 
    //If date is not passed, get current date 
    if(!date) 
    date = new Date(); 

    month = date.getMonth(); 

    // if month 2 digits (9+1 = 10) don't add 0 in front 
    return month < 9 ? "0" + (month+1) : month+1; 
} 
51

如果你想要像一個格式爲 「YYYY-MM-DDTHH:MM:SS」,那麼這可能會更快:

var date = new Date().toISOString().substr(0, 19); 
// toISOString() will give you YYYY-MM-DDTHH:mm:ss.sssZ 

或者常用的MySQL的日期時間格式「YYYY-MM- DD HH:MM:SS「:

var date2 = new Date().toISOString().substr(0, 19).replace('T', ' '); 

我希望這有助於

+0

這是我遇到的最簡單的解決方案。這裏唯一的問題是時區偏移。 – Praym 2016-08-26 15:22:26

+2

時區偏移量可以採取類似於: var date = new Date(new Date()。getTime() - new Date()。getTimezoneOffset()* 60 * 1000).toISOString()。substr 0,19).replace('T',''); – Praym 2016-08-26 15:47:54

+0

Praym,你的代碼適用於我,但複製和粘貼必須有一些隱藏的字符或東西,所以我只是手工輸入它。 – spacebread 2017-06-09 20:50:33

0

我的解決辦法:

function addLeadingChars(string, nrOfChars, leadingChar) { 
    string = string + ''; 
    return Array(Math.max(0, (nrOfChars || 2) - string.length + 1)).join(leadingChar || '0') + string; 
} 

用法:

var 
    date = new Date(), 
    month = addLeadingChars(date.getMonth() + 1), 
    day = addLeadingChars(date.getDate()); 

的jsfiddle:http://jsfiddle.net/8xy4Q/1/

12

要做到這一點,最好的方法是創建自己的簡單格式化(如下):

getDate()的當天返回月(從1-31)
getMonth()返回月份(從0-11)< 零基,0 =一月,11 =十二月
getFullYear()返回年份(四位數)< 不使用getYear()

function formatDateToString(date){ 
    // 01, 02, 03, ... 29, 30, 31 
    var dd = (date.getDate() < 10 ? '0' : '') + date.getDate(); 
    // 01, 02, 03, ... 10, 11, 12 
    var MM = ((date.getMonth() + 1) < 10 ? '0' : '') + (date.getMonth() + 1); 
    // 1970, 1971, ... 2015, 2016, ... 
    var yyyy = date.getFullYear(); 

    // create the format you want 
    return (dd + "-" + MM + "-" + yyyy); 
} 
0
var net = require('net') 

function zeroFill(i) { 
    return (i < 10 ? '0' : '') + i 
} 

function now() { 
    var d = new Date() 
    return d.getFullYear() + '-' 
    + zeroFill(d.getMonth() + 1) + '-' 
    + zeroFill(d.getDate()) + ' ' 
    + zeroFill(d.getHours()) + ':' 
    + zeroFill(d.getMinutes()) 
} 

var server = net.createServer(function (socket) { 
    socket.end(now() + '\n') 
}) 

server.listen(Number(process.argv[2])) 
1
function GetDateAndTime(dt) { 
    var arr = new Array(dt.getDate(), dt.getMonth(), dt.getFullYear(),dt.getHours(),dt.getMinutes(),dt.getSeconds()); 

    for(var i=0;i<arr.length;i++) { 
    if(arr[i].toString().length == 1) arr[i] = "0" + arr[i]; 
    } 

    return arr[0] + "." + arr[1] + "." + arr[2] + " " + arr[3] + ":" + arr[4] + ":" + arr[5]; 
} 
1

的答案在這裏是有幫助的,但是我需要更多的:不僅一個月,日期,月份,小時&秒,爲默認名稱。有趣的是,雖然前面所有的都需要「0」,但是「+ 1」只需要一個月,而不是其他的。

作爲例子:從MDN

("0" + (d.getMonth() + 1)).slice(-2)  // Note: +1 is needed 
("0" + (d.getHours())).slice(-2)   // Note: +1 is not needed 
1

提示:

function date_locale(thisDate, locale) { 
    if (locale == undefined) 
    locale = 'fr-FR'; 
    // set your default country above (yes, I'm french !) 
    // then the default format is "dd/mm/YYY" 

    if (thisDate == undefined) { 
    var d = new Date(); 
    } else { 
    var d = new Date(thisDate); 
    } 
    return d.toLocaleDateString(locale); 
} 

var thisDate = date_locale(); 
var dayN = thisDate.slice(0, 2); 
var monthN = thisDate.slice(3, 5); 
console.log(dayN); 
console.log(monthN); 

http://jsfiddle.net/v4qcf5x6/

1

而另一個版本在這裏https://jsfiddle.net/ivos/zcLxo8oy/1/,希望對大家有用。

var dt = new Date(2016,5,1); // just for the test 
var separator = '.'; 
var strDate = (dt.getFullYear() + separator + (dt.getMonth() + 1) + separator + dt.getDate()); 
// end of setup 

strDate = strDate.replace(/(\b\d{1}\b)/g, "0$1") 
3

使用Moment.js可以這樣做:

moment(new Date(2017, 1, 1)).format('DD') // day 
moment(new Date(2017, 1, 1)).format('MM') // month 
3

又一個例子,幾乎一行。

var date = new Date(); 
 
console.log((date.getMonth() < 9 ? '0': '') + (date.getMonth()+1));

相關問題