我确信这个问题之前已经得到解答,但如果可能的话,我希望在不包含外部 JS 库的情况下做到这一点,这就是我能找到的全部内容。
我需要按以下格式生成日期/时间:
2013 年 4 月 10 日,星期三 15:30:00
到目前为止,这是我的代码,但它没有提供您所看到的月份或日期名称:
<script type="text/javascript">
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year);
</script>
最佳答案
使用 getDay 和 getMonth 方法作为要使用的名称数组的索引-
var d = new Date();
var dayofweek=['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][d.getDay()],
date = d.getDate(),
month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()],
year = d.getFullYear();
document.write([dayofweek,date,month,year,d.toLocaleTimeString()].join(' '));
/* 返回值:(字符串) 2013 年 6 月 6 日星期四 12:44:36 */
关于javascript - 返回日期/时间函数中的日期和月份名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16965905/