Java Datestring,如何创建长星期几格式?
Java Datestring, how to create long Day of the Week format?
这是一个计算从当前时间算起的所需工作日数的脚本。它工作得很好。我希望它能将结果呈现为一周中的长字日,例如 Monday July 06 2015。当前结果是 Mon Jul 06 2015
我是个黑客猴子,所以低调的回应不会是侮辱!
Number.prototype.mod = function(n) {
return ((this % n) + n) % n;
}
Date.prototype.addBusDays = function(dd) {
var wks = Math.floor(dd / 5);
var dys = dd.mod(5);
var dy = this.getDay();
if (dy === 6 && dd > -1) {
if (dys === 0) {
dys -= 2;
dy += 2;
}
dys++;
dy -= 6;
}
if (dy === 0 && dd < 1) {
if (dys === 0) {
dys += 2;
dy -= 2;
}
dys--;
dy += 6;
}
if (dy + dys > 5) dys += 2;
if (dy + dys < 1) dys -= 2;
this.setDate(this.getDate() + wks * 7 + dys);
}
var today = new Date();
today.addBusDays(1);
document.getElementById('dtt').innerHTML = today.toDateString();
用法:我们在一个完整的句子中使用它:
Your package will arrive on or before <span id="dtt"></span>.
只需创建一个包含 month/day 个名称的数组,如下所示:
var days = ["Sunday","Monday",...];
var months = ["January", "February", ...];
然后
function formatDate(date) {
var day = date.getDate();
if(day < 10) {
day = "0" + day;
}
return days[date.getDay()] + " " + months[date.getMonth()] + " " + day + " " + date.getFullYear();
}
console.log(formatDate(new Date()));
这是一个计算从当前时间算起的所需工作日数的脚本。它工作得很好。我希望它能将结果呈现为一周中的长字日,例如 Monday July 06 2015。当前结果是 Mon Jul 06 2015
我是个黑客猴子,所以低调的回应不会是侮辱!
Number.prototype.mod = function(n) {
return ((this % n) + n) % n;
}
Date.prototype.addBusDays = function(dd) {
var wks = Math.floor(dd / 5);
var dys = dd.mod(5);
var dy = this.getDay();
if (dy === 6 && dd > -1) {
if (dys === 0) {
dys -= 2;
dy += 2;
}
dys++;
dy -= 6;
}
if (dy === 0 && dd < 1) {
if (dys === 0) {
dys += 2;
dy -= 2;
}
dys--;
dy += 6;
}
if (dy + dys > 5) dys += 2;
if (dy + dys < 1) dys -= 2;
this.setDate(this.getDate() + wks * 7 + dys);
}
var today = new Date();
today.addBusDays(1);
document.getElementById('dtt').innerHTML = today.toDateString();
用法:我们在一个完整的句子中使用它:
Your package will arrive on or before <span id="dtt"></span>.
只需创建一个包含 month/day 个名称的数组,如下所示:
var days = ["Sunday","Monday",...];
var months = ["January", "February", ...];
然后
function formatDate(date) {
var day = date.getDate();
if(day < 10) {
day = "0" + day;
}
return days[date.getDay()] + " " + months[date.getMonth()] + " " + day + " " + date.getFullYear();
}
console.log(formatDate(new Date()));