国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > 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程序設計有所幫助。

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

圖片精選

主站蜘蛛池模板: 阿坝县| 茌平县| 竹山县| 正安县| 丰县| 宁国市| 庆元县| 信阳市| 东光县| 荔浦县| 台中市| 通江县| 永州市| 焉耆| 靖西县| 霍邱县| 正安县| 连江县| 辽阳县| 盱眙县| 绥宁县| 茶陵县| 韶山市| 碌曲县| 平武县| 石楼县| 张掖市| 浦城县| 太康县| 东光县| 攀枝花市| 泾川县| 罗田县| 裕民县| 海伦市| 巴东县| 兰西县| 南溪县| 名山县| 九江县| 临泽县|