gpt4 book ai didi

来自 EventBrite 的带时区的 Javascript 日期

转载 作者:行者123 更新时间:2023-11-30 18:01:38 27 4
gpt4 key购买 nike

基本上我的目标是将字符串正确解析为日期,我已经完成了大部分工作,因为我将日期和时间转换为 JavaScript 日期对象,但我不确定如何解析和应用时区.

我正在使用的 EventBrite API 为 start_date 和 timezone_offset 返回一个字符串:

timezone_offset: "GMT-0500"

start_date: "2013-04-27 19:00:00"

我正在使用这段似乎运行良好的代码解析 start_date:

var sd = response.events[i].event.start_date;
var parsedSD = sd.split(/[:-\s]/);
var startDate = new Date(parsedSD[0], parsedSD[1]-1, parsedSD[2], parsedSD[3], parsedSD[4], parsedSD[5]);

我缺少的部分是如何解析 timezone_offset 或以其他方式将其应用于日期,这样如果我以本地时间显示时间/日期,它是正确的吗?

感谢任何提示,我最初的想法是获取最后五个字符的子字符串,然后使用它来使用 getTime/setTime 的组合来抵消时间。

老实说,这并不重要,因为大多数计划参加事件的人都在同一时区,或者我可以只显示 timezone_offset,但无论如何我想知道如何正确执行此操作。

编辑 1

这是我当前的代码,我在注释中说明了 Safari 认为这些值是什么:

var timezone_offset = response.events[i].event.timezone_offset; //timezone_offset is "GMT-500"
var sd = response.events[i].event.start_date+timezone_offset; //sd is "2013-04-27 19:00:00GMT-500"
var dt_iso8601 = sd.replace(/ /, 'T').replace(/GMT/,''); //dt_iso8601 is "2013-04-27T19:00:00-0500"
var startDate = new Date(dt_iso8601); //startDate is Invalid Date

编辑 2

对于发现此问题并遇到此特定问题的任何人,这里是我用来忽略过去任何事件的完整解决方案(因为目前看来 EventBrite API 没有为您提供执行此操作的方法).这是我正在处理的 AngularJS Controller ,但 JS 应该主要适用于任何上下文:

[index.php]

<script src="scripts/moment.min.js" type="text/javascript"></script>
<script src="scripts/Eventbrite.jquery.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js"></script>
<script type="text/javascript" src="eventDirective.js"></script>

[事件指令.js]

function EventCtrl($scope)
{
$scope.events=[];
$scope.noEventsDisplay = "Loading events...";

Eventbrite({'app_key': "YOURAPPKEYHERE"}, function(eb){

// define a few parameters to pass to the API
// Options are listed here: http://developer.eventbrite.com/doc/organizers/organizer_list_events/
var options = {
'id' : "your_organizer_id_here",

};

// provide a callback to display the response data:
eb.organizer_list_events( options, function( response ){
validEvents = [];
var now = moment();
for(var i = 0; i<response.events.length; i++)
{
var timezone_offset = response.events[i].event.timezone_offset;

var ed = response.events[i].event.end_date+timezone_offset;
var em = moment(ed,format);

//if older than today skip
if(em<now)
continue;
var sd = response.events[i].event.start_date+' '+timezone_offset;
var format = 'YYYY-MM-DD HH:mm:ss [GMT]ZZ';
var sm = moment(sd,format);

response.events[i].event.formattedDate = sm.toDateString();
validEvents.push(response.events[i])
}
if(validEvents.length == 0)
{
$scope.$apply(function(scope){scope.noEventsDisplay = "No upcoming events to display, please check back soon.";});
}
else
{
$scope.$apply(function(scope){scope.noEventsDisplay = "";});
}
$scope.$apply(function(scope){scope.events = validEvents;});

//$('.event_list').html(eb.utils.eventList( response, eb.utils.eventListRow ));
});
});
}

最佳答案

如果您想要一个出色的跨浏览器解决方案而不需要自己动手,请尝试 moment.js :

// you can define a particular format to use when parsing
var format = 'YYYY-MM-DD HH:mm:ss [GMT]ZZ';

// then just pass in your input
var m = moment(start_date + ' ' + timezone_offset, format);

// for example:
var m = moment('2013-04-27 19:00:00 GMT-0500', format);

关于来自 EventBrite 的带时区的 Javascript 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16843570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com