I need your help,
我需要你的帮助,
I can't seem to find any other help on this on the internet, because it seems its either one way ot the other. What I would like to be able to do is to create a combined, two-fold javascript function that would convert a long date string into the mm-dd-yyyy format, and when the same function is called again with no string specified to convert, to just return todays date in mm-dd-yyyy format.
我似乎无法在互联网上找到任何其他帮助,因为它似乎是另一种方式。我希望能够做的是创建一个组合的,双重的javascript函数,它将长日期字符串转换为mm-dd-yyyy格式,并且当再次调用相同的函数时没有指定要转换的字符串,以mm-dd-yyyy格式返回今天的日期。
Example:
getDate(Fri May 22 2015 13:32:25 GMT-0400)
would return: 05-22-2015
将返回:2015年5月22日
getDate()
would return today's date of 05-23-2015
将于今天05-23-2015返回
3 个解决方案
#1
0
Hi this should do the trick
嗨,这应该做的伎俩
FORMAT: mm-dd-yyyy
function addZeroIfRequired(dayOrMonth) {
return dayOrMonth > 9 ? dayOrMonth : "0"+dayOrMonth;
}
function getDate(dateString) {
var date = dateString ? new Date(dateString) : new Date();
return addZeroIfRequired((date.getUTCMonth()+1)) + "-" +
addZeroIfRequired(date.getDate())+ "-" +
date.getUTCFullYear();
}
console.log(getDate()); // 05-23-2015
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400")); 05-22-2015
NOTE: the +1 after the getUTCMonth()
.
注意:getUTCMonth()之后的+1。
JSFiddle. Open the console to see the result. https://jsfiddle.net/wt79yLo0/2/
的jsfiddle。打开控制台以查看结果。 https://jsfiddle.net/wt79yLo0/2/
ISO FORMAT: yyyy-mm-dd
ISO格式:yyyy-mm-dd
Just in case someone is interested in the opposite format, the code would be much nicer and neater:
为了防止有人对相反的格式感兴趣,代码会更好更整洁:
function getDate(dateString) {
var date = dateString ? new Date(dateString) : new Date();
return date.toISOString().substring(0, 10);
}
console.log(getDate());
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400"));
#2
0
First I would recommend a very powerful library for JS called Moment.js which solves all this kind of problems.
首先,我会推荐一个非常强大的JS库,名为Moment.js,它解决了所有这些问题。
But if you only want a snippet, here is my proposal:
但如果你只想要一个片段,这是我的建议:
function twoDigits(num) {
return ("0" + num).substr(-2);
}
function getFormattedDateDMY(date, separator) {
var day = twoDigits(date.getDate());
var month = twoDigits(date.getMonth());
var year = date.getFullYear();
return [day,month,year].join(separator);
}
function getFormattedDateMDY(date, separator) {
var day = twoDigits(date.getDate());
var month = twoDigits(date.getMonth());
var year = date.getFullYear();
return [month,day,year].join(separator);
}
console.log(getFormattedDateDMY(new Date(), "-")); //23-04-2015
console.log(getFormattedDateMDY(new Date(), "-")); //04-23-2015
#3
0
With getDate()
, getMonth()
and getFullYear()
. You have to set a "0" before the months and days which are < 10
. GetMonth()
starts with 0, therefore (getMonth() + 1)
.
使用getDate(),getMonth()和getFullYear()。您必须在<10之前的月份和日期之前设置“0”.GetMonth()以0开头,因此(getMonth()+ 1)。
function getFormattedDate() {
var date = new Date();
var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
var year = date.getFullYear();
var formattedDate = day + "-" + month + "-" + year;
return formattedDate;
}
console.log(getFormattedDate());
#1
0
Hi this should do the trick
嗨,这应该做的伎俩
FORMAT: mm-dd-yyyy
function addZeroIfRequired(dayOrMonth) {
return dayOrMonth > 9 ? dayOrMonth : "0"+dayOrMonth;
}
function getDate(dateString) {
var date = dateString ? new Date(dateString) : new Date();
return addZeroIfRequired((date.getUTCMonth()+1)) + "-" +
addZeroIfRequired(date.getDate())+ "-" +
date.getUTCFullYear();
}
console.log(getDate()); // 05-23-2015
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400")); 05-22-2015
NOTE: the +1 after the getUTCMonth()
.
注意:getUTCMonth()之后的+1。
JSFiddle. Open the console to see the result. https://jsfiddle.net/wt79yLo0/2/
的jsfiddle。打开控制台以查看结果。 https://jsfiddle.net/wt79yLo0/2/
ISO FORMAT: yyyy-mm-dd
ISO格式:yyyy-mm-dd
Just in case someone is interested in the opposite format, the code would be much nicer and neater:
为了防止有人对相反的格式感兴趣,代码会更好更整洁:
function getDate(dateString) {
var date = dateString ? new Date(dateString) : new Date();
return date.toISOString().substring(0, 10);
}
console.log(getDate());
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400"));
#2
0
First I would recommend a very powerful library for JS called Moment.js which solves all this kind of problems.
首先,我会推荐一个非常强大的JS库,名为Moment.js,它解决了所有这些问题。
But if you only want a snippet, here is my proposal:
但如果你只想要一个片段,这是我的建议:
function twoDigits(num) {
return ("0" + num).substr(-2);
}
function getFormattedDateDMY(date, separator) {
var day = twoDigits(date.getDate());
var month = twoDigits(date.getMonth());
var year = date.getFullYear();
return [day,month,year].join(separator);
}
function getFormattedDateMDY(date, separator) {
var day = twoDigits(date.getDate());
var month = twoDigits(date.getMonth());
var year = date.getFullYear();
return [month,day,year].join(separator);
}
console.log(getFormattedDateDMY(new Date(), "-")); //23-04-2015
console.log(getFormattedDateMDY(new Date(), "-")); //04-23-2015
#3
0
With getDate()
, getMonth()
and getFullYear()
. You have to set a "0" before the months and days which are < 10
. GetMonth()
starts with 0, therefore (getMonth() + 1)
.
使用getDate(),getMonth()和getFullYear()。您必须在<10之前的月份和日期之前设置“0”.GetMonth()以0开头,因此(getMonth()+ 1)。
function getFormattedDate() {
var date = new Date();
var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
var year = date.getFullYear();
var formattedDate = day + "-" + month + "-" + year;
return formattedDate;
}
console.log(getFormattedDate());