JavaScript日期/时间/文本格式

时间:2017-09-18 20:29:55

标签: javascript

我有一些不推荐使用的JavaScript可用,但使用document.write(),并希望更新它。我尝试了document.getElementById("greeting").innerHTML = d.toString();,但这不起作用。我在网上彻底搜索过但没有运气。我已经包含了JavaScript时间脚本。非常感谢您的帮助。丰富。



var d = new Date();
var dateText = "";
var dayValue = d.getDay();
if (dayValue === 0)
  dateText += "Sunday";
else if (dayValue === 1)
  dateText += "Monday";
else if (dayValue === 2)
  dateText += "Tuesday";
else if (dayValue === 3)
  dateText += "Wednesday";
else if (dayValue === 4)
  dateText += "Thursday";
else if (dayValue === 5)
  dateText += "Friday";
else if (dayValue === 6)
  dateText += "Saturday";

var monthValue = d.getMonth();
dateText += " ";
if (monthValue === 0)
  dateText += "January";
if (monthValue === 1)
  dateText += "February";
if (monthValue === 2)
  dateText += "March";
if (monthValue === 3)
  dateText += "April";
if (monthValue === 4)
  dateText += "May";
if (monthValue === 5)
  dateText += "June";
if (monthValue === 6)
  dateText += "July";
if (monthValue === 7)
  dateText += "August";
if (monthValue === 8)
  dateText += "September";
if (monthValue === 9)
  dateText += "October";
if (monthValue === 10)
  dateText += "November";
if (monthValue === 11)
  dateText += "December";

if (navigator.appName.indexOf('Microsoft') !== -1)
  dateText += " " + d.getDate() + ", " + (1900 + d.getYear());
else if (navigator.appName.indexOf('Netscape') !== -1)
  dateText += " " + d.getDate() + ", " + (1900 + d.getYear());
else if (navigator.appName.indexOf('Opera') !== -1)
  dateText += " " + d.getDate() + ", " + (1900 + d.getYear());

var minuteValue = d.getMinutes();
if (minuteValue < 10)
  minuteValue = "0" + minuteValue

var hourValue = d.getHours();
if (hourValue < 12) {
  greeting = "Good morning!";
  var timeText = " at " + hourValue + ":" + minuteValue +
    " AM. &nbsp;Welcome!";

} else if (hourValue === 12) {
  var greeting = "Good afternoon!";
  timeText = " at " + hourValue + ":" + minuteValue +
    " PM. &nbsp;Welcome! ";
} else if (hourValue < 17) {
  greeting = "Good afternoon!";
  timeText = " at " + (hourValue - 12) + ":" + minuteValue +
    " PM. &nbsp;Welcome! ";
} else {
  greeting = "Good evening!";
  timeText = " at " + (hourValue - 12) + ":" + minuteValue +
    " PM. &nbsp;Welcome! ";
}
document.open();
document.getElementById("greeting").innerHTML = d.toString();
// ORIGINAL - document.write(greeting + " It's " + dateText + timeText);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)

您缺少的是页面上具有ID greeting的元素,这是 document.getElementById("greeting") 所必需的。您无法设置DOM中不存在的元素的 .innerHTML

您也不需要document.open()

以下是添加了greeting元素的工作示例:

var d = new Date();
var dateText = "";
var dayValue = d.getDay();
if (dayValue === 0)
  dateText += "Sunday";
else if (dayValue === 1)
  dateText += "Monday";
else if (dayValue === 2)
  dateText += "Tuesday";
else if (dayValue === 3)
  dateText += "Wednesday";
else if (dayValue === 4)
  dateText += "Thursday";
else if (dayValue === 5)
  dateText += "Friday";
else if (dayValue === 6)
  dateText += "Saturday";

var monthValue = d.getMonth();
dateText += " ";
if (monthValue === 0)
  dateText += "January";
if (monthValue === 1)
  dateText += "February";
if (monthValue === 2)
  dateText += "March";
if (monthValue === 3)
  dateText += "April";
if (monthValue === 4)
  dateText += "May";
if (monthValue === 5)
  dateText += "June";
if (monthValue === 6)
  dateText += "July";
if (monthValue === 7)
  dateText += "August";
if (monthValue === 8)
  dateText += "September";
if (monthValue === 9)
  dateText += "October";
if (monthValue === 10)
  dateText += "November";
if (monthValue === 11)
  dateText += "December";

if (navigator.appName.indexOf('Microsoft') !== -1)
  dateText += " " + d.getDate() + ", " + (1900 + d.getYear());
else if (navigator.appName.indexOf('Netscape') !== -1)
  dateText += " " + d.getDate() + ", " + (1900 + d.getYear());
else if (navigator.appName.indexOf('Opera') !== -1)
  dateText += " " + d.getDate() + ", " + (1900 + d.getYear());

var minuteValue = d.getMinutes();
if (minuteValue < 10)
  minuteValue = "0" + minuteValue

var hourValue = d.getHours();
if (hourValue < 12) {
  greeting = "Good morning!";
  var timeText = " at " + hourValue + ":" + minuteValue +
    " AM. &nbsp;Welcome!";

} else if (hourValue === 12) {
  var greeting = "Good afternoon!";
  timeText = " at " + hourValue + ":" + minuteValue +
    " PM. &nbsp;Welcome! ";
} else if (hourValue < 17) {
  greeting = "Good afternoon!";
  timeText = " at " + (hourValue - 12) + ":" + minuteValue +
    " PM. &nbsp;Welcome! ";
} else {
  greeting = "Good evening!";
  timeText = " at " + (hourValue - 12) + ":" + minuteValue +
    " PM. &nbsp;Welcome! ";
}
document.getElementById("greeting").innerHTML = d.toString();
<div id="greeting"></div>

希望这有帮助! :)