如何使用div在javascript中显示当前月份,年份 [英] how to display current month, year in javascript using div
问题描述
我正在尝试获取一个jquery或java-script代码,用于在div中显示当前月份和年份,但到目前为止还无法使用。我的意思是我想以这种格式显示当前月份和年份: 2012年10月
以便每个月我都不需要编辑它或任何东西。
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中显示变量。
I saw many questions here, but none shows how to display the variable in a div.
任何想法如何实现?
非常感谢您的帮助和想法
Your help and ideas are much appreciated
推荐答案
JavaScript没有'本地实现 strftime
,所以你必须做一些不那么优雅的事情:
JavaScript doesn't natively implement strftime
, so you'll have to do something a bit less elegant:
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();
};
我假设你有一个 id $的元素c $ c>
date
HTML中的某个地方。
I'm assuming that you have an element with an id
of date
somewhere in your HTML.
这篇关于如何使用div在javascript中显示当前月份,年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!