当前位置:网站首页>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
边栏推荐
- [appium] write scripts by designing Keyword Driven files
- HCIP第五次实验
- Index: teach you index from zero basis to proficient use
- [binary number] maximum depth of binary tree + maximum depth of n-ary tree
- Add animation to the picture under V-for timing
- 开期货,开户云安全还是相信期货公司的软件?
- 编译原理 求first集 follow集 select集预测分析表 判断符号串是否符合文法定义(有源码!!!)
- 587. Install fence / Sword finger offer II 014 Anagrams in strings
- flink 学习(十二)Allowed Lateness和 Side Output
- 极致体验,揭晓抖音背后的音视频技术
猜你喜欢

Cross domain settings of Chrome browser -- including new and old versions

2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory

Open source key component multi_ Button use, including test engineering

The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time

Dry goods | how to extract thumbnails quickly?

EasymodbusTCP之clientexample解析

【Appium】通过设计关键字驱动文件来编写脚本

On the problem of V-IF display and hiding

uni-app黑马优购项目学习记录(下)
随机推荐
Utilisation de la liste - Ajouter, supprimer et modifier la requête
油猴网站地址
440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
Construction of functions in C language programming
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
Qt 修改UI没有生效
The method of changing a value in the array and a value in the object of wechat applet
Compare the performance of query based on the number of paging data that meet the query conditions
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
一些问题一些问题一些问题一些问题
31. Next arrangement
Leak detection and vacancy filling (VIII)
48. 旋转图像
JS get link? The following parameter name or value, according to the URL? Judge the parameters after
Sword finger offer 22 The penultimate node in the linked list - speed pointer
394. String decoding - auxiliary stack
102. Sequence traversal of binary tree
Listen for click events other than an element
PC电脑使用无线网卡连接上手机热点,为什么不能上网
This point in JS