javascript 如何使用 jquery 打印日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20165212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:54:49  来源:igfitidea点击:

How to print date and time with jquery

javascriptjquerydate

提问by alias51

Simple question: how can I display a browser's current date and time in the format below?:

简单的问题:如何以下面的格式显示浏览器的当前日期和时间?:

<h2>[current time, e.g. 02.32am]</h2>
<h4>[day of week, e.g. saturday]</h4>
<h3>[full date, e.g. 11 November 2013]</h3>

回答by vogomatix

Create a Javascript Date object and display the values from that.

创建一个 Javascript Date 对象并显示其中的值。

var today = new Date();
// What happens next depends on whether you want UTC or locale time...
// assuming locale time in this example...
$('#time').html( today.getHours() + ':' + today.getMinutes());
$('#weekday').html(today.toDateString().substring(0,3));
$('#date').html( today.toDateString() );
<h2 id="time"></h2>
<h4 id="weekday"></h4>
<h3 id="date"></h3>

Alternatively use DatePickerfrom JQueryUIto generate your date strings e.g.

或者使用的DatePickerjQueryUI的生成您的日期字符串如

 $('#date').html( $.datepicker.formatDate( "dd MM YY", today ) );
 $('#weekday').html( $.datepicker.formatDate( "DD", today ));

Remember to load jQueryUI to do this. Datepicker is quite powerful and has many other useful abilities including locale handling

请记住加载 jQueryUI 来执行此操作。Datepicker 非常强大,并具有许多其他有用的功能,包括区域设置处理

回答by Joke_Sense10

Try this:

试试这个:

html:

html:

<h2 id="para1"></h2>
<h4 id="para2"></h4>
<h3 id="para3"></h3>

Javascript:

Javascript:

var date = new Date();   //Creates date object
var hours = date.getHours();   //get hour using date object
var minutes = date.getMinutes();    //get minutes using date object
var ampm = hours >= 12 ? 'pm' : 'am';  //Check wether 'am' or 'pm'

var month = date.getMonth(); //get month using date object
var day = date.getDate();    //get day using date object
var year = date.getFullYear();  //get year using date object
var dayname = date.getDay();  // get day of particular week

var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];

var week=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; 

document.getElementById("para1").innerHTML = hours+"."+minutes+ampm;
document.getElementById("para2").innerHTML = week[dayname];
document.getElementById("para3").innerHTML = day+" "+monthNames[month]+" "+year;

Check demo here: Demo

在此处查看演示:演示

回答by Krish R

var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();




 var Month=new Array(12);
 Month[0]="January";
 Month[1]="Feb";
 .
 .

 Month[11]="December";

 var MonthName = Month[d.getMonth()]; 

var today = year +" "+MonthName+" "+day; 

 alert(today);

var t=d.toLocaleTimeString();

document.getElementById('time').innerHTML = t;

     var weekday=new Array(7);
     weekday[0]="Sunday";
     weekday[1]="Monday";
     weekday[2]="Tuesday";
     weekday[3]="Wednesday";
     weekday[4]="Thursday";
     weekday[5]="Friday";
     weekday[6]="Saturday";

     var weekdayinfo = weekday[d.getDay()]; 

     document.getElementById('weekday').innerHTML = weekdayinfo;


   document.getElementById('date').innerHTML = today;


 <h2 id="time"></h2>
 <h4 id="weekday"></h4>
  <h3 id="date"></h3>

Ref: http://www.jquery4u.com/snippets/jquery-todays-date-ddmmyyyy/

参考:http: //www.jquery4u.com/snippets/jquery-todays-date-ddmmyyyy/