简体   繁体   English

在 javascript 中创建日历

[英]creating a calendar in javascript

I am in the middle of creating a calendar in javascript but am contemplating using the date functions to retrieve values or splitting the date string to retrieve values.我正在 javascript 中创建日历,但正在考虑使用日期函数检索值或拆分日期字符串以检索值。 I tried looking up the big o for these functions but have not found any.我试着查找这些函数的大 o,但没有找到。 I want to know what is fastest.我想知道什么是最快的。

For example例如

var date = new Date();
date.toString().split(" ")[1]; //this will get month name

vs对比

var date = new Date();
date.getMonth() //this will get month

they might not be the same but my question is which one is faster.它们可能不一样,但我的问题是哪个更快。 edit i forgot toString.编辑我忘了toString。

As the split() method use searching by pattern algorithm and returns an Array which is f(n) = n = O(n) [Big O notation] it is good performance wise.由于 split() 方法使用模式算法搜索并返回 f(n) = n = O(n) [大 O 符号] 的数组,因此性能良好。

But Date.prototype.getMonth() involve no searching and return the Number of the month – 1 as it starts counting at 0. (Source: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date/getMonth ) f(n) = n = O(1).但 Date.prototype.getMonth() 不涉及搜索并返回月数 - 1,因为它从 0 开始计数。(来源: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/ Objets_globaux/Date/getMonth ) f(n) = n = O(1)。

Conclusion: Date.getMonth is faster in time and space complexity then.split() method结论:Date.getMonth 在时间和空间复杂度上更快 then.split() 方法

I would use the getMount() method, it's a built in the object so it's more reasonable to me to use it along with all the other methods.我会使用 getMount() 方法,它内置于 object 中,因此对我来说将它与所有其他方法一起使用更为合理。 When you say "faster" you mean write less code or the code execution is faster?当您说“更快”时,您是指编写更少的代码还是代码执行得更快?

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

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