javascript - 如何从sql server数据库创建日期和时间的倒计时?(javascript - how to create countdown to date and time from sql server database?)
这是我第一次在javascript中使用倒计时,所以我对这个主题进行了一些研究,我发现了一些有趣的链接:“ 如何倒计时到日期 ”和“ https://www.sitepoint.com/build-javascript-countdown -timer-no-dependencies / “,但我的问题是,如果我想从数据库中获取数据和时间,我该怎么做? 例如:我有一个带有ID,Event,StartDate,StartTime和EndTime的表事件。 我怎样才能改变:“var end = new Date('02 / 19/2012 10:1 AM');” 从第一个链接或这个:“自动安排时钟”从第二个到倒计时时间,直到具有最近时间和日期的事件,依此类推。 请记住,我是一个非常棒的人,所以请耐心等待。 对不起任何拼写错误。 谢谢!
更新:
这是控制器部分:
[HttpGet] public JsonResult GetEvent(int Id) { BOL1.IMS2Entities db = new BOL1.IMS2Entities(); var ev = from e in db.tbl_Event where e.ID == Id select e; //return Json(ev.FirstOrDefault(), JsonRequestBehavior.AllowGet); return Json(Id, JsonRequestBehavior.AllowGet); }
[HttpGet] public JsonResult GetEvent(int Id) { BOL1.IMS2Entities db = new BOL1.IMS2Entities(); var ev = from e in db.tbl_Event where e.ID == Id select e; //return Json(ev.FirstOrDefault(), JsonRequestBehavior.AllowGet); return Json(Id, JsonRequestBehavior.AllowGet); }
这是脚本部分:<script> function GetEvent() { debugger; $.ajax({ type: "GET", url: "Home/GetEvent", data: { Id: ID }, contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { debugger; alert(result) }, error: function (response) { debugger; alert('error'); } }); } var tbl_Event.StartDate = 'yyyy-MM-dd hh:hh:hh'; //var ServerDate_Time = '2017-02-17 10:45:00'; //console.log(CompareDateTime(ServerDate_Time)); console.log(CompareDateTime(tbl_Event.StartDate + tbl_Event.StartTime)) //function CompareDateTime (ServerDateTime){ function CompareDateTime (tbl_Event.StartDate + tbl_Event.StartTime){ var dateString = new Date(); var currentTime = new Date(parseInt(dateString)); var month = ('0' + (currentTime.getMonth() + 1)).slice(-2) var day = ('0' + (currentTime.getDate())).slice(-2) var year = currentTime.getFullYear(); var hours = ('0' + (currentTime.getHours())).slice(-2) var min = ('0' + (currentTime.getMinutes())).slice(-2) var sec = ('0' + (currentTime.getSeconds())).slice(-2) var date = year + "-" + month + "-" + day + " " + hours + ":" + min + ":" + sec; if(ServerDateTime == date){ return true; }else { return false; } } } </script>
It's my first time using a countdown in javascript so I have researched a little about this topic and I found some interesting links: "how to countdown to a date" and "https://www.sitepoint.com/build-javascript-countdown-timer-no-dependencies/", but my question is if I want to get data and time from the database, how can I do that? E.g.: I have a table Event with ID,Event,StartDate,StartTime and EndTime. How can I change from this: "var end = new Date('02/19/2012 10:1 AM');" from the first link or this: "Schedule the Clock Automatically" from the second to countdown time until event with the most recent time and date, and so on. Please keep in mind that I'm a total nooby so please bear with me. Sorry for any misspelling. Thank you!
Update:
This is the controller part:
[HttpGet] public JsonResult GetEvent(int Id) { BOL1.IMS2Entities db = new BOL1.IMS2Entities(); var ev = from e in db.tbl_Event where e.ID == Id select e; //return Json(ev.FirstOrDefault(), JsonRequestBehavior.AllowGet); return Json(Id, JsonRequestBehavior.AllowGet); }
and this is the scripting part:<script> function GetEvent() { debugger; $.ajax({ type: "GET", url: "Home/GetEvent", data: { Id: ID }, contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { debugger; alert(result) }, error: function (response) { debugger; alert('error'); } }); } var tbl_Event.StartDate = 'yyyy-MM-dd hh:hh:hh'; //var ServerDate_Time = '2017-02-17 10:45:00'; //console.log(CompareDateTime(ServerDate_Time)); console.log(CompareDateTime(tbl_Event.StartDate + tbl_Event.StartTime)) //function CompareDateTime (ServerDateTime){ function CompareDateTime (tbl_Event.StartDate + tbl_Event.StartTime){ var dateString = new Date(); var currentTime = new Date(parseInt(dateString)); var month = ('0' + (currentTime.getMonth() + 1)).slice(-2) var day = ('0' + (currentTime.getDate())).slice(-2) var year = currentTime.getFullYear(); var hours = ('0' + (currentTime.getHours())).slice(-2) var min = ('0' + (currentTime.getMinutes())).slice(-2) var sec = ('0' + (currentTime.getSeconds())).slice(-2) var date = year + "-" + month + "-" + day + " " + hours + ":" + min + ":" + sec; if(ServerDateTime == date){ return true; }else { return false; } } } </script>
原文:https://stackoverflow.com/questions/42292950
最满意答案
pathlib
模块支持/
用于连接路径对象。>>> p = Path('/etc') >>> q = p / 'init.d' / 'reboot' >>> q PosixPath('/etc/init.d/reboot') >>> q.resolve() PosixPath('/etc/rc.d/init.d/halt')
作为如何使用
__div__
,这是一个扩展str
的简单类。 它应该工作只要至少一个参数是MyPath
一个实例(即,它不适用于两个普通字符串)。class MyPath(str): def __div__(self, other): assert isinstance(other, str) return os.path.join(self, other) def __rdiv__(self, other): assert isinstance(other, str) return os.path.join(other, self) # Using __div__ print MyPath("/home/bob") / MyPath("bin") print MyPath("/home/bob") / "bin" # Using __rdiv__ print "/home/bob" / MyPath("bin")
The
pathlib
module supports/
for concatenating path objects.>>> p = Path('/etc') >>> q = p / 'init.d' / 'reboot' >>> q PosixPath('/etc/init.d/reboot') >>> q.resolve() PosixPath('/etc/rc.d/init.d/halt')
As an example of how to use
__div__
, here's a simple class that extendsstr
. It should work as long as at least one argument is an instance ofMyPath
(i.e., it won't work with two plain strings).class MyPath(str): def __div__(self, other): assert isinstance(other, str) return os.path.join(self, other) def __rdiv__(self, other): assert isinstance(other, str) return os.path.join(other, self) # Using __div__ print MyPath("/home/bob") / MyPath("bin") print MyPath("/home/bob") / "bin" # Using __rdiv__ print "/home/bob" / MyPath("bin")
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
Python:next()函数(Python: next() function)[2022-01-16]
表达式(sum(row) for row in M)创建了所谓的生成器 。 该生成器将为M每一行评估表达式( sum(row) )一次。 然而,发电机还没有做任何事情,我们只是设置了它。 next(G)的语句next(G)实际上在M上运行生成器。 所以,如果你运行next(G)一次,你会得到第一行的总和。 如果再次运行它,则会得到第二行的总和,以此类推。 >>> M = [[1,2,3], ... [4,5,6], ... [7,8,9]] >>> >>> G = (sum(row) ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
Python中的Gamma函数(Gamma Function in Python)[2022-03-13]
是的你可以。 在模块math有一些特殊的功能编码,如Gamma功能。 看看这里 Yes, you can. In the module math there are special functions coded such as the Gamma function. Look at here -
python中的sum函数(sum function in python)[2023-11-04]
当你这样做 from SomeModule import * 你确实凌驾于内置和函数。 这会将numpy命名空间中的所有内容导入默认命名空间。 您可能想要做的是: import numpy as np 然后,您可以将numpy点函数作为np.dot访问。 通过这种方式,如果它们定义了相同名称的函数,则可以使命名空间不会相互踩踏。 另一个选择,如果你只想要点功能就是这样做: from numpy import dot 然后点函数是numpy中唯一可用的函数。 这些是使用其他模块时采用的两种主要方法。 导 ... -
何时使用*和**作为python函数中的函数参数(When to use * and ** as a function argument in python function)[2023-02-23]
正如您所说的那样,您没有在参数中使用它们(在调用函数时会出现这种情况),您在创建函数时出现的参数中使用它们。 *和**运算符在每种情况下都有不同的用途。 在定义函数时,它们指定位置参数将放在元组中,并且关键字参数将放在dict中。 是的我只是说了一些论点,但在这种情况下它们适用于参数。 def example(*args, **kwargs): print "args: {0}".format(args) print "kwargs: {0}".format(kwargs) example ... -
Python中的时间函数(Time function in python)[2023-07-01]
这是适用于计算的科学符号。 E-05意味着10的权力-5: 8.73194...E-05 = 8.73194 * 10^(-5) = 0.000 087 319 4... That's scientific notation applied to computing. E-05 means 10 to the power of -5: 8.73194...E-05 = 8.73194 * 10^(-5) = 0.000 087 319 4... -
只有当所有的元素都是特鲁蒂 。 Truthy!= True 。 基本上检查bool(something)是否为True (对于迭代中的所有something )。 >>> "?" == True False >>> "?" == False # it's not False either False >>> bool("?") True only when all the elements are Truthy. Truthy != True. all essentially checks whether ...
-
python中的函数注释(function annotations in python)[2022-09-26]
如果您使用python3.5,最好的方法是使用typing.Union >>> from typing import Union >>> import numpy as np >>> def fun(data: Union[np.ndarray, list]): pass 如果你一直使用Union [t1,t2,...],你也可以使用typing.TypeVar。 (另外,您可以比代码中的许多联盟更容易地添加和删除TypeVar中的类型) >>> from typing import Ty ... -
python中的函数(Function in python)[2023-09-29]
它是函数的“成员变量”,因此每次调用它时(除非用seed调用时),序列将选择它停止的位置。 It is a "member variable" of the function, so that each time it is called (except when called with something for seed) the sequence will pick of where it left off.