我想将日期转换为字符串格式,包括日期名称和月份名称,如果我正在通过日期,例如01/09/2016那么它应该像2016年9月1日星期四那样呈现为9月1日的日期名称,最艰巨的任务是得到日期的日期名称。我怎样才能实现这一目标?
答案 0 :(得分:1)
您可以使用JavaScript轻松完成此操作。请尝试以下代码段:
var d = new Date("2016-09-01");
console.log(d.toDateString());
输出结果为:
2016年9月1日星期四
答案 1 :(得分:0)
你可以试试这个
<script>
function myFunction() {
var s = '2016/09/01';//this may be user entered string
var d = new Date(s);
var n = d.toString();
console.log(n);
}
</script>
答案 2 :(得分:0)
以下字符串传递Thursday 1 September 2016
时,以下重新调整'2016-09-01'
。
请注意,本周的全名Thursday
不仅会被返回,而且缩写为Thu
;
(function() {
var dateFormat = function(ourDate) {
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
date = new Date(ourDate),
year = date.getFullYear(),
indexMonth = date.getMonth(),
dayWeek = date.getDay(),
day = date.getDate();
return weekDays[dayWeek] + ' ' + day + ' ' + months[indexMonth] + ' ' + year;
};
console.log(dateFormat('2016-09-01'));
})();
答案 3 :(得分:0)
您需要dd / mm / yyyy所需的格式本身不支持您需要使用像momentjs这样的东西来解析各种不同的格式,然后您可以按照建议使用toString
答案 4 :(得分:0)
javascript本机日期对象可以从字符串日期构造。但它支持&#39; mm / dd / yyyy&#39;格式。因此,首先将日期格式转换为&#39; mm / dd / yyyy&#39;格式。然后你可以构造javascript日期对象。之后,您可以使用getDay和getMonth函数来获取星期和月份的日期。
function getDayOfDate (date){
var arr = date.split('/');
var newFormat=arr[1]+"/"+arr[0]+"/"+arr[2];
var date = new Date(newFormat);
return date.getDay();
}
&#13;
答案 5 :(得分:0)
您可以延长日期时间
Date.prototype.Format = function() {
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 weekday = weekday[this.getDay()];
var day = this.getDay();
if(day === 1)
day += day + "st"
else day+= "th"
var months = new Array(12);
months[0]= " January ";
months[1] = " February ";
months[2] = " Mars ";
months[3] = " April";
months[4] = " May ";
months[5] = " June ";
months[6] = " July ";
months[7] = " August ";
months[8] = " September ";
months[9] = " October ";
months[10] = " November ";
months[11] = " December ";
var month = months[this.getMonth()]
return weekday + day + month + this.getFullYear();
};
var d = new Date();
console.log(d.Format());
答案 6 :(得分:0)
使用下面的代码,你会得到你提到的日期字符串。
在某些情况下,您获得的上一个日期早于您在var d = new Date("2016-09-01");
中输入的日期Eastern Daylight Time
。
那么,这就是你如何得到确切的日期字符串格式`输入代码
var doo = new Date("2011-09-24");
console.log(new Date(doo.getTime() + doo.getTimezoneOffset()*60000));
这里是小提琴链接:pressme