javascript 完整日历 获取当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29548097/
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
Full Calendar Get current date
提问by user3868832
I've been learning Ruby over the last year and I'm very new to JS so I'll try to explain this as best I can.
去年我一直在学习 Ruby,我对 JS 很陌生,所以我会尽我所能解释这一点。
I am using Adam Shaw's full calendar plugin. All I want to do is get the current month I am viewing (and use that to limit how far in the future or past a user can navigate, but that's not the problem).
我正在使用Adam Shaw 的完整日历插件。我想要做的就是获取我正在查看的当前月份(并使用它来限制用户在未来或过去可以导航的距离,但这不是问题)。
I can get the current date, sort of. But, because of my lack of JS knowledge I'm not sure how to access the date.
我可以得到当前日期,有点。但是,由于我缺乏 JS 知识,我不确定如何访问日期。
Here is the relevant section of the config file,
这是配置文件的相关部分,
viewRender: function(view){
var maxDate = "<%= finish.strftime('%Y/%m/%d') %>";
var currentDate = $('#calendar').fullCalendar('getDate');
console.log(currentDate);
if (view.start > maxDate){
header.disableButton('prev');
}
}
When I inspect the console log I see this being output as I click through the months.
当我检查控制台日志时,我看到这是在我点击月份时输出的。
So as you can see it is displaying the current date in view. My question is how do I access the _d
bit of the Moment
variable so I can use it?
如您所见,它正在显示当前日期。我的问题是如何访问变量的_d
位Moment
以便我可以使用它?
My understanding would be that the Moment is class instance and the stuff in the dropdown is like its attributes, would this be a correct interpretation?
我的理解是 Moment 是类实例,下拉列表中的内容就像它的属性,这是正确的解释吗?
回答by Patel
FullCalendar's getDate
returns a moment object, so you need moment's toDate()
method to get date out of it.
FullCalendargetDate
返回一个 moment 对象,因此您需要moment 的toDate()
方法来从中获取日期。
So, in you code try:
所以,在你的代码中尝试:
console.log(currentDate.toDate());
and that should return a date object.
这应该返回一个日期对象。
回答by Piter Novian
To get the current date of the calendar, do:
要获取日历的当前日期,请执行以下操作:
var tglCurrent = $('#YourCalendar').fullCalendar('getDate');
This will return the date as a moment
object. You can then format it as a string in the usual date format like so:
这会将日期作为moment
对象返回。然后,您可以将其格式化为通常的日期格式的字符串,如下所示:
var tgl=moment(tglCurrent).format('YYYY-MM-DD');
For the actual time, use the format: YYYY-MM-DD LTS
对于实际时间,使用格式: YYYY-MM-DD LTS
回答by brandelizer
var moment = $('#YourCalendar').fullCalendar('getDate');
var calDate = moment.format('DD.MM.YYYY HH:mm'); //Here you can format your Date