简体   繁体   English

如何在fullcalendar event.start方法上解析事件开始时间?

[英]How to parse event start time on fullcalendar event.start method?

Hello I'm using the fullcalendar jQuery plugin to create a calendar app. 您好,我正在使用全日历jQuery插件来创建日历应用。 When I call the start method on the event object the date format I get is like this: 当我在事件对象上调用start方法时,我得到的日期格式如下:

ISO FORMAT: ISO格式:

Sun Nov 27 2011 06:30:00 GMT+0200 (EET) 太阳2011年11月27日06:30:00 GMT + 0200(EET)

Now I want to keep only the time and minutes of the event: 现在,我只想保留事件的时间和分钟:

6 hours 6小时

30 minutes 30分钟

in our example. 在我们的例子中。

How can I trim/parse only the time with JavaScript? 如何使用JavaScript修剪/解析时间?

You can use Date.parse: 您可以使用Date.parse:

var your_date = "Sun Nov 27 2011 06:30:00 GMT+0200 (EET)";
var t = Date.parse(your_date); // date in milliseconds since January 1, 1970
var d = new Date(t);
d.getHours(); // 6 hours
d.getMinutes(); // 30 minutes

Take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp to more JavaScript Date options. 查看http://www.w3schools.com/jsref/jsref_obj_date.asp ,了解更多JavaScript日期选项。

You can use Date.parse but when used with new Date you obtain a Date object in the current timezone , so with different hour values on different computers. 您可以使用Date.parse但是当与new Date一起使用时,您将在当前时区中获得一个Date对象,因此在不同的计算机上具有不同的小时值。

A more robust option is using regular expressions. 一个更强大的选项是使用正则表达式。 If the format is always like you posted, then the hours can be obtained by this regular expression: 如果格式始终像您发布的那样,则可以通过此正则表达式获取小时数:

/ (\d+):/

which means: 意思是:

  1. a space 空间
  2. one or more digits 一个或多个数字
  3. a colon 冒号

For the minutes it is something similar, but those are surrounded with a colon at both sides: 在分钟上,这是相似的,但是它们的两边都用冒号包围:

/:(\d+):/

You can then parse the numbers out via .exec(str)[1] , where 1 is group 1, defined with the parens. 然后,您可以通过.exec(str)[1]解析数字,其中1是第1组,并用括号定义。

Converting to a number can be done using +str : 可以使用+str转换为数字:

var str     = "Sun Nov 27 2011 06:30:00 GMT+0200 (EET)",
    hours   = +/ (\d+):/.exec(str)[1]; // 6
    minutes = +/:(\d+):/.exec(str)[1]; // 30

The answer is a bit complex, because you can simply use the string in the javascript Date() constructor like this: 答案有点复杂,因为您可以像这样在javascript Date()构造函数中简单地使用字符串:

var d = new Date("Sun Nov 27 2011 06:30:00 GMT+0200 (EET)");

But doing a call to d.getHours() will probably not result in the value you wanted to get, because JavaScript converts the date to your local timezone. 但是,调用d.getHours()可能不会产生您想要获取的值,因为JavaScript将日期转换为本地时区。 For example, doing alert(d) for me in Germany results in: 例如,在德国为我做alert(d)导致:

Sun Nov 27 2011 05:30:00 GMT+0100 (CET)

If that is what you wanted, you're done. 如果这是您想要的,那么您就完成了。 Otherwise, you will need to convert it the the timezone wanted. 否则,您将需要将其转换为所需的时区。

I am not much into jQuery (I try to avoid it), so I assume they did the right thing and stored a Date instance. 我对jQuery的了解不多(我尽量避免使用它),所以我假设他们做对了事并存储了Date实例。 If that is the case, you are only seeing the (localized) string representation of that Date instance (as if you called its toString() method), and you can retrieve the components using its prototype methods: 如果是这种情况,您只会看到该Date实例的(本地化) 字符串表示形式 (就像您调用了它的toString()方法一样),并且可以使用其原型方法来检索组件:

var d = jQuery(…).whatever(…);
var hours = d.getHours();
var minutes = d.getMinutes();

or 要么

var hours = d.getUTCHours();
var minutes = d.getUTCMinutes();

depending on what you are interested in. No parsing necessary. 取决于您感兴趣的内容。无需解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM