当前位置:网站首页>Encapsulate a timestamp to date method on string prototype
Encapsulate a timestamp to date method on string prototype
2022-04-23 17:49:00 【Sanxiaochi 513】
Just learning React course , There is a section about DOM Of diffing Algorithm , The teacher's example shows the current time , But he didn't show it completely , But with toTimeString() , And I've encapsulated this method before : Time stamp to format date
When I use it, I think about it , In this way, it is a function , Can it be like toTimeString In this way, it is encapsulated in the string ? Do as you say , Knock out the following code on the browser console :
String.prototype.toDate = function(){
???
}
Suddenly confused , How can I get the value ??
Normal function calls can take values through formal parameters , But this is the way I point out , Since it was pointed out , In function scope this Is it the caller of this function, that is, this string ?
But now it has become this, How can I get its original value ? Don't think too much. , There must be some native way to get its value , Immediately type the following code :
String.prototype
It is as expected , I found a suspicious guy on a bunch of properties and methods of the prototype ——valueOf
'123'.valueOf() // '123'
Then the problem of getting value is solved !! The rest is to copy the previous code and change it a little
String.prototype.toDate = function () {
var date = new Date(Number(this.valueOf().length == 13 ? this.valueOf() : this.valueOf() + '000'));
let Y = date.getFullYear() + '-';
let M = (date.getMonth() + 1).toString().padStart(2, '0') + '-';
let D = (date.getDate()).toString().padStart(2, '0') + ' ';
let h = (date.getHours()).toString().padStart(2, '0') + ':';
let m = (date.getMinutes()).toString().padStart(2, '0') + ':';
let s = (date.getSeconds()).toString().padStart(2, '0');
return Y + M + D + h + m + s;
}
In this way, you can point out the formatting time happily ~·o·~
Of course ,Number Type needs to be transformed first , I won't give it Number Another prototype
版权声明
本文为[Sanxiaochi 513]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230550325135.html
边栏推荐
- 386. 字典序排数(中等)-迭代-全排列
- Leak detection and vacancy filling (6)
- Construction of functions in C language programming
- Index: teach you index from zero basis to proficient use
- 2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
- Future 用法详解
- On the method of outputting the complete name of typeID from GCC
- 土地覆盖/利用数据产品下载
- 2021长城杯WP
- The system cannot be started after AHCI is enabled
猜你喜欢

JVM class loading mechanism

Welcome to the markdown editor
![SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]](/img/60/e4d47d458dd98a0c6ba51874e07c30.png)
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]

Kubernetes 服务发现 监控Endpoints
![Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag](/img/ed/4d61ce34f830209f5adbddf9165676.png)
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag

2021 Great Wall Cup WP

Halo open source project learning (II): entity classes and data tables

PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet

开源按键组件Multi_Button的使用,含测试工程

Future usage details
随机推荐
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
MySQL进阶之索引【分类,性能分析,使用,设计原则】
2021 Great Wall Cup WP
1217_使用SCons生成目标文件
SystemVerilog (VI) - variable
双闭环直流调速系统matlab/simulink仿真
Gets the time range of the current week
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
209. 长度最小的子数组-滑动窗口
剑指 Offer 03. 数组中重复的数字
Add animation to the picture under V-for timing
122. 买卖股票的最佳时机 II-一次遍历
92. 反转链表 II-字节跳动高频题
Sword finger offer 22 The penultimate node in the linked list - speed pointer
Use of list - addition, deletion, modification and query
Detailed deployment of flask project
Halo 开源项目学习(二):实体类与数据表
Open source key component multi_ Button use, including test engineering
470. 用 Rand7() 实现 Rand10()
超分之TDAN