问题
I have the below java-script to display the current date in the given format Mon Jun 2 17:54:28 UTC+0530 2014 in a hta(html application), now I want to make this appear in a way like Welcome the current date of my system: Mon Jun 2 17:54:28 UTC+0530 2014 and this text should be a having scrollable affects for eg: one moving from right to left.
I tried to use the below tag to get a scrollable text but how can I call this java-script variable in the <marquee>
tag so that I get the today's date and time also as a part of the scrollable affects but it is not working for my HTML page.
Kindly let me know how to rectify this issue
HTML CODE:
<marquee behavior="scroll" bgcolor="yellow" loop="-1" width="30%">
<i><font color="blue"><strong>Welcome</strong> Today's date is : </font></i>
</marquee>
JAVASCRIPT TO DISPLAY THE CURRENT DATE AND TIME:
<script language="javascript">
var today = new Date();
document.write(today);
</script>
回答1:
Method 1:
With marquee
tag.
HTML
<marquee behavior="scroll" bgcolor="yellow" loop="-1" width="30%">
<i>
<font color="blue">
Today's date is :
<strong>
<span id="time"></span>
</strong>
</font>
</i>
</marquee>
JS
var today = new Date();
document.getElementById('time').innerHTML=today;
Fiddle demo here
Method 2:
Without marquee
tag and with CSS
.
HTML
<p class="marquee">
<span id="dtText"></span>
</p>
CSS
.marquee {
width: 350px;
margin: 0 auto;
background:yellow;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
color:blue;
font-size:18px;
}
.marquee span {
display: inline-block;
padding-left: 100%;
text-indent: 0;
animation: marquee 15s linear infinite;
}
.marquee span:hover {
animation-play-state: paused
}
@keyframes marquee {
0% { transform: translate(0, 0); }
100% { transform: translate(-100%, 0); }
}
JS
var today = new Date();
document.getElementById('dtText').innerHTML=today;
Fiddle demo here
回答2:
This will help you.
Javascript
debugger;
var today = new Date();
document.getElementById('date').innerHTML = today
Fiddle Demo
回答3:
Try this one:
HTML:
<div id="para1"></div>
JavaScript:
document.getElementById("para1").innerHTML = formatAMPM();
function formatAMPM() {
var d = new Date(),
minutes = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes() : d.getMinutes(),
hours = d.getHours().toString().length == 1 ? '0'+d.getHours() : d.getHours(),
ampm = d.getHours() >= 12 ? 'pm' : 'am',
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
return days[d.getDay()]+' '+months[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+hours+':'+minutes+ampm;
}
Result:
Mon Sep 18 2017 12:40pm
回答4:
<script>
var today = new Date;
document.getElementById('date').innerHTML= today.toDateString();
</script>
回答5:
<div id="clockbox" style="font:14pt Arial; color:#FF0000;text-align: center; border:1px solid red;background:cyan; height:50px;padding-top:12px;"></div>
来源:https://stackoverflow.com/questions/23994748/display-the-current-date-and-time-using-html-and-javascript-with-scrollable-effe