麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > JavaScript > 正文

JavaScript通過Date-Mask將日期轉換成字符串的方法

2024-05-06 16:21:22
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JavaScript通過Date-Mask將日期轉換成字符串的方法,涉及javascript日期、數組及字符串操作的相關技巧,需要的朋友可以參考下

本文實例講述了JavaScript通過Date-Mask將日期轉換成字符串的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. var MonthNames = ["January""February""March""April""May""June""July""August""September""October""November""December"]; 
  2. var DayNames = [ "Sunday""Monday""Tueday""Wednesday""Thursday",  
  3. "Friday""Saturday" ]; 
  4. var ShortMths = ["Jan""Feb""Mar""Apr""May""Jun""Jul""Aug",  
  5. "Sep""Oct""Nov""Dec"]; 
  6. var ShortDays = ["Sun""Mon""Tue""Wed""Thu""Fri""Sat"]; 
  7. var StringToDate = function (sDate, sFormat, cutOff) { 
  8. // Input: a date value as a string, it's format as a string e.g. 'dd-mmm-yy' 
  9. // Optional: a cutoff (integer) for 2 digit years. 
  10. // If no 'd' appears in the format string then the 1st of the month is assumed. 
  11. // If the year is 20 and the cut-off is 30 then the value will be converted  
  12. // to 2020; if the year is 40 then this will be converted to 1940. 
  13. // If no cut-off is supplied then '20' will be pre-pended to the year (YY). 
  14. // Output: a string in the format 'YYYY/MM/DD' or '' 
  15. // Will not attempt to convert certain combinations e.g. DMM, MDD, DDM, YYYYD. 
  16. var sParsed, fndSingle; 
  17. // sParsed will be constructed in the format 'YYYY/MM/DD' 
  18. sDate = sDate.toString().toUpperCase(); 
  19. sFormat = sFormat.toUpperCase(); 
  20. if (sFormat.search(/MMMM|MMM/) + 1) { // replace Mar/March with 03, etc. 
  21. sDate = sDate.replace(new RegExp('(' + ShortMths.join('|') + ')[A-Z]*''gi'), 
  22. function (m) { 
  23. var i = ShortMths.indexOf(m.charAt(0).toUpperCase() +  
  24. m.substr(1, 2).toLowerCase()) + 1; 
  25. return ((i < 10) ? "0" + i : "" + i).toString(); 
  26. }); 
  27. sFormat = sFormat.replace(/MMMM|MMM/g, 'MM'); 
  28. if (sFormat.search(/DDDD|DDD/) + 1) { // replace Tue/Tuesday, etc. with '' 
  29. sDate = sDate.replace(new RegExp('(' + ShortDays.join('|') + ')[A-Z]*''gi'),''); 
  30. sFormat = sFormat.replace(/DDDD|DDD/g, ''); 
  31. sDate = sDate.replace(/(^|/D)(/d)(?=/D|$)/g, function($0, $1, $2) { 
  32. // single digits 2 with 02 
  33. return $1 + '0' + $2; 
  34. }); 
  35. sFormat = sFormat.replace(/(^|[^DMY])(D|M)(?=[^DMY]|$)/g, function($0, $1, $2){ 
  36. return $1 + $2 + $2; // replace D or M with DD and MM 
  37. }); 
  38. // are there still single Ds or Ms? 
  39. fndSingle = sFormat.search(/(^|[^D])D([^D]|$)|(^|[^M])M([^M]|$)/)+1; 
  40. // do not attempt to parse, for example, 'DMM' 
  41. if ( fndSingle ) return ''
  42. sFormat = sFormat.replace(/(^|[^Y])(YY)(?=[^Y]|$)/g, function($0, $1, $2, index) { 
  43. var tempDate = sDate.substr(0, index + 1); 
  44. tempDate += (cutOff) ? ((parseInt(sDate.substr(index + 1, 2),10) > cutOff) ? '19' : '20') : '20'
  45. tempDate += sDate.substr(index + 1); 
  46. sDate = tempDate; 
  47. return $1 + $2 + $2; 
  48. }); 
  49. sParsed = ('YYYY/MM/DD').replace(/YYYY|MM|DD/g, function(m){ 
  50. return (sFormat.indexOf(m) + 1) ?  
  51. sDate.substr(sFormat.indexOf(m), m.length) : ''
  52. }); 
  53. if (sParsed.charAt(0) == '/') { 
  54. // if no year specified, assume the current year 
  55. sParsed = (new Date().getFullYear()) + sParsed; 
  56. if (sParsed.charAt(sParsed.length - 1) == '/') { 
  57. // if no date, assume the 1st of the month 
  58. sParsed += '01'
  59. // should end up with 10 characters.. 
  60. return ( sParsed.length == 10 ) ? sParsed : ''
  61. }; 

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 成人免费福利视频 | 日韩黄色片免费看 | 国产精品久久久久影院老司 | 黄在线观看在线播放720p | xxxxhdvideosex| 高颜值美女啪啪 | www.国产一区.com | 亚州综合网 | 日本黄色免费观看视频 | 久久露脸国语精品国产91 | 99欧美精品 | 国产电影av在线 | 国产亚洲高清视频 | 国产日韩久久久久69影院 | 日韩在线毛片 | 国产一级在线看 | 国产亚洲综合一区二区 | h色视频在线观看 | 成人一区二区在线观看视频 | 一本一道久久久a久久久精品91 | 久久毛片免费观看 | 欧美性生活视频免费 | 久久精品a一级国产免视看成人 | 中国美女一级黄色大片 | 国产精品成年片在线观看, 激情小说另类 | 成年人观看免费视频 | 久草在线资源视频 | 北京一级毛片 | 999插插插| 国产区二区 | 天天透天天狠天天爱综合97 | 亚州视频在线 | 手机在线看片国产 | 色婷婷久久久久久 | 在线免费小视频 | 超污视频在线看 | 福利免费在线 | 综合网天天射 | 欧美日韩大片在线观看 | 亚洲91网 | 色操网|