javascript - How to parse event start time on fullcalendar event.start method? - Stack Overflow

Hello I'm using the fullcalendar jQuery plugin to create a calendar app. When I call the start met

Hello I'm using the fullcalendar jQuery plugin to create a calendar app. When I call the start method on the event object the date format I get is like this:

ISO FORMAT:

Sun Nov 27 2011 06:30:00 GMT+0200 (EET)

Now I want to keep only the time and minutes of the event:

6 hours

30 minutes

in our example.

How can I trim/parse only the time with JavaScript?

Hello I'm using the fullcalendar jQuery plugin to create a calendar app. When I call the start method on the event object the date format I get is like this:

ISO FORMAT:

Sun Nov 27 2011 06:30:00 GMT+0200 (EET)

Now I want to keep only the time and minutes of the event:

6 hours

30 minutes

in our example.

How can I trim/parse only the time with JavaScript?

Share edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 27, 2011 at 11:43 Andrew KordampalosAndrew Kordampalos 4231 gold badge7 silver badges11 bronze badges 1
  • Also If I want to parse the day "Sun"? Because the getDay method returns the day as digit 0-6. – Andrew Kordampalos Commented Nov 27, 2011 at 12:04
Add a ment  | 

4 Answers 4

Reset to default 3

You can use 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./jsref/jsref_obj_date.asp to more JavaScript Date options.

The answer is a bit plex, because you can simply use the string in the javascript Date() constructor like this:

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. For example, doing alert(d) for me in Germany results in:

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.

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 puters.

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.

Converting to a number can be done using +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

I am not much into jQuery (I try to avoid it), so I assume they did the right thing and stored a Date instance. 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 ponents using its prototype methods:

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744651732a4585940.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信