Javascript Moment.js 从日期中获取日期名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27669019/
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
Moment.js get day name from date
提问by Clément Andraud
I'm using jquery and moment.js for a custom calendar.
我将 jquery 和 moment.js 用于自定义日历。
I have a date object in a variable myDatelike :
我在一个变量中有一个日期对象,myDate例如:
Object { date="2014-12-23 14:00:00", timezone_type=3, timezone="Europe/Paris"}
I want, using moment.js (or not) get the day name of this date, in my example i need to get : tuesday
我想,使用 moment.js(或不使用)获取此日期的日期名称,在我的示例中,我需要获取: tuesday
Ideas ? Thanks
想法?谢谢
回答by Paul
With moment you can parse the date string you have:
随着时刻,您可以解析您拥有的日期字符串:
var dt = moment(myDate.date, "YYYY-MM-DD HH:mm:ss")
That's for UTC, you'll have to convert the time zone from that point if you so desire.
这是UTC,如果您愿意,您必须从该点转换时区。
Then you can get the day of the week:
然后你可以得到一周中的哪一天:
dt.format('dddd');
回答by Deepu Reghunath
code
代码
var mydate = "2017-06-28T00:00:00";
var weekDayName = moment(mydate).format('dddd');
console.log(weekDayName);
mydate is the input date. The variable weekDayName get the name of the day. Here the output is
mydate 是输入日期。变量 weekDayName 获取一天的名称。这里的输出是
Output
输出
Wednesday
周三
var mydate = "2017-08-30T00:00:00";
console.log(moment(mydate).format('dddd'));
console.log(moment(mydate).format('ddd'));
console.log('Day in number[0,1,2,3,4,5,6]: '+moment(mydate).format('d'));
console.log(moment(mydate).format('MMM'));
console.log(moment(mydate).format('MMMM'));
<script src="https://momentjs.com/downloads/moment.js"></script>
回答by Anand S
var mydate = "2017-06-28T00:00:00";
var weekDayName = moment(mydate).format('ddd');
console.log(weekDayName);
Result: Wed
结果:周三
var mydate = "2017-06-28T00:00:00";
var weekDayName = moment(mydate).format('dddd');
console.log(weekDayName);
Result: Wednesday
结果:周三

