Javascript显示当前日期和时间

我有以下测试脚本来显示当前日期和时间:-

我有以下测试脚本来显示当前日期和时间:-

document.getElementById("para1").innerHTML = formatAMPM();
function formatAMPM() {
    var date = new Date();
    var hours = date.getHours();
    var days = date.getDay(); 
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0'+minutes : minutes;
    var strTime = date + ' ' + hours + ':' + minutes + ' ' + ampm;
    return strTime;
}

这将显示以下内容:-

Fri Aug 30 2013 16:36:10 T+0100 (T Standard Time) 4:36 pm

但我需要修改这个只显示:-

Fri Aug 30 2013 4:36 pm

任何人都可以建议我怎么能做到这一点?

48

使用Console.Log的演示

// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// log the date in the browser console
console.log('date:', n);
// log the time in the browser console
console.log('time:',time);

使用DIV的演示

// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// find the html element with the id of time
// set the innerHTML of that element to the date a space the time
document.getElementById('time').innerHTML = n + ' ' + time;
<div id='time'></div>

注意:这些功能不完全跨浏览器支持

跨浏览器功能

//Fri Aug 30 2013 4:36 pm
console.log(formatAMPM(new Date()));
//using your function (passing in date)
function formatAMPM(date) {
    // gets the hours
    var hours = date.getHours();
    // gets the day
    var days = date.getDay();
    // gets the month
    var minutes = date.getMinutes();
    // gets AM/PM
    var ampm = hours >= 12 ? 'pm' : 'am';
    // converts hours to 12 hour instead of 24 hour
    hours = hours % 12;
    // converts 0 (midnight) to 12
    hours = hours ? hours : 12; // the hour '0' should be '12'
    // converts minutes to have leading 0
    minutes = minutes < 10 ? '0'+ minutes : minutes;
  
    // the time string
    var time = hours + ':' + minutes + ' ' + ampm;
  
    // gets the match for the date string we want
    var match = date.toString().match(/\w{3} \w{3} \d{1,2} \d{4}/);
  
    //the result
    return match[0] + ' ' + time;
}
28

试试这个:

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;
DEMO
9

更新为使用更现代的 Luxon 而不是 MomentJS。

不要重新发明轮子。使用经过测试和测试的库为您执行此操作,例如 Luxon:https://moment.github.io/luxon/index.html

从他们的网站:

https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-method-toLocaleString
//=> 'Thu, Apr 20, 11:27 AM'
DateTime.local().toLocaleString({ weekday: 'short', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' });
3
(function(con) {
    var oDate = new Date();
    var nHrs = oDate.getHours();
    var nMin = oDate.getMinutes();
    var nDate = oDate.getDate();
    var nMnth = oDate.getMonth();
    var nYear = oDate.getFullYear();
    con.log(nDate + ' - ' + nMnth + ' - ' + nYear);
    con.log(nHrs + ' : ' + nMin);
})(console);

这会产生一个输出,如:

30 - 8 - 2013
21 : 30

也许你可以参考documentation on Date object在 MDN 的更多信息

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(321)
基于日期范围参考单元格调整相关性(correlations in excel)
上一篇
我如何开始使用Sparql作为.NET开发人员
下一篇

相关推荐

  • javascript作品:利用JavaScript实现动态数据可视化

    示例示例作品是指使用语言编写的脚本,可以在网页上实现动态效果。例如:一个简单的脚本,可以用来显示当前时间:…

    2023-08-20 12:19:17
    0 68 65
  • javascript交互效果:使用JavaScript轻松实现交互效果

    JavaScript交互效果是指,当用户与网页进行交互时,网页会有相应的反应,以及动态效果。例如:鼠标移动到一个元素上,元素就会改变颜色;点击某个按钮,网页就会显示新的内容;滚动页面,某个元素就会固定在页面上,等等。…

    2023-05-07 12:38:50
    0 80 71
  • java和js的区别:Java与JavaScript的异同点比较

    示例示例Java和之间有很多明显的区别,主要如下:Java是一种面向对象的编程语言,而是一种脚本语言。…

    2023-11-19 10:40:20
    0 46 28
  • javascript入门学习学习如何使用JavaScript

    JavaScript是一种开放的、跨平台的、轻量级的脚本语言,它可以用来编写网页应用程序。JavaScript入门学习的步骤如下:…

    2023-06-22 05:43:40
    0 78 68
  • javascript 替换字符串 Hello JavaScript

    JavaScript 替换字符串可以使用 String.prototype.replace() 方法来实现。该方法接受两个参数,第一个参数是要被替换的子字符串,第二个参数是用来替换的字符串。…

    2023-05-03 03:39:37
    0 92 70
  • javascript 正则表达式 在线 John Smith

    JavaScript 正则表达式在线是一种用于搜索和替换文本的强大工具。它们可以用来验证文本,查找特定的字符串,替换文本,或者用于数据提取等等。…

    2023-05-12 14:25:27
    0 97 66
  • javascript垃圾回收机制如何优化内存使用

    JavaScript垃圾回收机制是一种自动内存管理机制,它可以帮助开发人员释放不再使用的内存。它可以跟踪变量和对象,并在不再需要时自动释放内存。…

    2023-04-30 03:43:42
    0 23 25
  • javascript的输出语句:使用JavaScript输出结果

    JavaScript的输出语句是指在网页中显示文本或其他内容的语句,常用的输出语句有document.write()、window.alert()和console.log()。…

    2023-02-09 10:17:17
    0 39 46

发表评论

登录 后才能评论

评论列表(41条)