Javascript 如何使用div在javascript中显示当前月份,年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12757911/
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
how to display current month, year in javascript using div
提问by Digital site
I'm trying to get a jquery or java-script code for displaying the current month and year in a div, but unable so far. I mean I want to display the current month and year in this format:October 2012
so that every month I don't need to edit it or anything.
我正在尝试获取用于在 div 中显示当前月份和年份的 jquery 或 java 脚本代码,但到目前为止还无法。我的意思是我想以这种格式显示当前的月份和年份:October 2012
这样每个月我都不需要编辑它或任何东西。
I saw many questions here, but none shows how to display the variable in a div.
我在这里看到了很多问题,但没有一个显示如何在 div 中显示变量。
Any idea how to accomplish that?
知道如何做到这一点吗?
Your help and ideas are much appreciated
非常感谢您的帮助和想法
回答by Blender
JavaScript doesn't natively implement strftime
, so you'll have to do something a bit less elegant:
JavaScript 本身并不实现strftime
,因此您必须做一些不太优雅的事情:
window.onload = function() {
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];;
var date = new Date();
document.getElementById('date').innerHTML = months[date.getMonth()] + ' ' + date.getFullYear();
};
I'm assuming that you have an element with an id
of date
somewhere in your HTML.
我假设您的 HTML 中有一个带有id
of的元素date
。
回答by FosterZ
there's no inbuild function in javascript to get full name of month. You can create an array of month names and use it to get full month name for e.g
javascript 中没有 inbuild 函数来获取月份的全名。您可以创建一个月份名称数组并使用它来获取完整的月份名称,例如
var m_names = ['January', 'February', 'March',
'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
d = new Date();
var n = m_names[d.getMonth()];
回答by chovy
回答by Parth Jasani
Get current Date, month, year or ETC....
获取当前日期、月份、年份或 ETC....
new Date().getDate() // Get the day as a number (1-31)
new Date().getDay() // Get the weekday as a number (0-6)
new Date().getFullYear() // Get the four digit year (yyyy)
new Date().getHours() // Get the hour (0-23)
new Date().getMilliseconds() // Get the milliseconds (0-999)
new Date().getMinutes() // Get the minutes (0-59)
new Date().getMonth() // Get the month (0-11)
new Date().getSeconds() // Get the seconds (0-59)
new Date().getTime() // Get the time (milliseconds since January 1, 1970)
回答by scorpionsrk7
Using this you can get the only current month
使用它,您可以获得当前唯一的月份
var d = new Date();
var n = d.getMonth()+1;
the value of n is 3 (like current month is march)
n 的值为 3(比如当前月份是三月)
回答by nbrooks
var dt = new Date();
var m = ['January','February', 'March', ...][dt.getMonth()]; //you get the idea...
var y = dt.getFullYear();
$('<div>').html(m + " " + y).appendTo(document);
回答by McPepper
You can format your date with JqueryUI (if you already use this). Datepicker provides formatting:
您可以使用 JqueryUI 格式化您的日期(如果您已经使用了它)。日期选择器提供格式:
$.datepicker.formatDate("MM yy", date);
To insert your date in a div you can use this function:
要在 div 中插入日期,您可以使用此功能:
$("#divId").append($.datepicker.formatDate("MM yy", new Date()));