以开始日期作为函数变量的计数计时器

发布于 2024-11-04 15:31:00 字数 571 浏览 1 评论 0原文

我是 javascript 的新手,正在尝试创建一个计数计时器,它可以将某个日期作为输入并计算自该日期以来经过的时间。

我目前正在使用 http://keith-wood.name/countdown.html,但我遇到了 2 个问题

1)我不知道如何将输入日期添加为函数变量。我在这个论坛上发现了另一个类似的问题 - Jquery Countup Timer,但他们使用的是固定日期与可变输入日期相反。

2)如何确保变量输入的日期符合特定的格式?目前我正在尝试从 mysql 数据库中读取输入日期,该数据库将日期时间戳记为 2010-06-17 15:12:30,但我不认为这是jquery插件将读取的日期格式。有没有办法解析这个日期以使格式匹配?

非常感谢任何可以提供帮助的人!

史蒂夫

I'm new to javascript and was trying to create a countup timer that could take a certain date as an input and count the time that has passed since that date.

I'm currently using a jquery plugin from http://keith-wood.name/countdown.html, but I'm having 2 problems

1) I can't figure out how to add the input date as a function variable. I found another question on this forum that was similar - Jquery Countup Timer, but they were using a fixed date as opposed to a variable input date.

2) How do I ensure that the variable input date conforms to a specific format? Currently I'm trying to read the input date out of a mysql database which timestamps the dates as
2010-06-17 15:12:30, but I don't think that this is the date format that the jquery plugin will read. Is there a way to parse this date so that the formats match up?

Thanks very much to anyone that can help!

Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木緿 2024-11-11 15:31:00

这是一个例子。

http://jsfiddle.net/HXf5v/

您所需要做的就是创建一个获取日期的 JavaScript 函数对象作为参数

,例如:

function doDateCountUp(date){
  $('#sinceCountDown').countdown({since: date, compact:true, format: 'YOWDHMS', description: ''});    
}

或者,您也可以始终创建一个 jquery 函数。

jQuery.fn.doDateCountUp = function(date){
    $(this).countdown({since: date, compact:true, format: 'YOWDHMS', description: ''});  
}

然后你可以像这样调用 jquery 函数:

$('#mydiv').doDateCountUp(new Date())

至于格式化一个新的 Date() 对象。你应该看看:

http://www.w3schools.com/jS/js_obj_date.asp< /a>

表明您可以创建一个日期对象,如下所示:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Here is an example.

http://jsfiddle.net/HXf5v/

All you need to do is create a javascript function that takes the Date object as a parameter

something like:

function doDateCountUp(date){
  $('#sinceCountDown').countdown({since: date, compact:true, format: 'YOWDHMS', description: ''});    
}

Alternatively, you could also always create a jquery function.

jQuery.fn.doDateCountUp = function(date){
    $(this).countdown({since: date, compact:true, format: 'YOWDHMS', description: ''});  
}

and then you can invoke the jquery function like so:

$('#mydiv').doDateCountUp(new Date())

As for formatting a new Date() object. you should take a look at:

http://www.w3schools.com/jS/js_obj_date.asp

which states that you can create a date object like this:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
情泪▽动烟 2024-11-11 15:31:00

以下是从文本框中获取日期并将其用作开始日期的示例:

使用输入示例进行计数

javascript 可以通过接受几种不同格式的字符串来创建新的日期。以下是这些格式的快速链接:javascript 日期

Here is an example taking in the date from a textbox and using it as the startdate:

countup with input exmample

javascript can create a new date by taking in a string in a few different formats. Here's a quick link to those formats: javascript date

孤单情人 2024-11-11 15:31:00

将 mysql 日期转换为 javascript 日期对象:

function mysql2jsdate(mysqlDate){
   var parts = mysqlDate.replace(/:/g, '-').replace(' ', '-').split("-");
   parts = $.map(parts, function(part){ return parseInt(part, 10) });
   return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]);
}

使用示例:

var jsDate = mysql2jsdate('2010-06-17 15:12:30');

希望这有助于实现您的目标

To transform the mysql date to a javascript date object:

function mysql2jsdate(mysqlDate){
   var parts = mysqlDate.replace(/:/g, '-').replace(' ', '-').split("-");
   parts = $.map(parts, function(part){ return parseInt(part, 10) });
   return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]);
}

Example of usage:

var jsDate = mysql2jsdate('2010-06-17 15:12:30');

Hope this helps to achieve your goal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文