首页 > 语言 > 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. } 
  29. if (sFormat.search(/DDDD|DDD/) + 1) { // replace Tue/Tuesday, etc. with '' 
  30. sDate = sDate.replace(new RegExp('(' + ShortDays.join('|') + ')[A-Z]*', 'gi'),''); 
  31. sFormat = sFormat.replace(/DDDD|DDD/g, ''); 
  32. } 
  33. sDate = sDate.replace(/(^|/D)(/d)(?=/D|$)/g, function($0, $1, $2) { 
  34. // single digits 2 with 02 
  35. return $1 + '0' + $2; 
  36. }); 
  37. sFormat = sFormat.replace(/(^|[^DMY])(D|M)(?=[^DMY]|$)/g, function($0, $1, $2){ 
  38. return $1 + $2 + $2; // replace D or M with DD and MM 
  39. }); 
  40. // are there still single Ds or Ms? 
  41. fndSingle = sFormat.search(/(^|[^D])D([^D]|$)|(^|[^M])M([^M]|$)/)+1; 
  42. // do not attempt to parse, for example, 'DMM' 
  43. if ( fndSingle ) return ''; 
  44. sFormat = sFormat.replace(/(^|[^Y])(YY)(?=[^Y]|$)/g, function($0, $1, $2, index) { 
  45. var tempDate = sDate.substr(0, index + 1); 
  46. tempDate += (cutOff) ? ((parseInt(sDate.substr(index + 1, 2),10) > cutOff) ? '19' : '20') : '20'; 
  47. tempDate += sDate.substr(index + 1); 
  48. sDate = tempDate; 
  49. return $1 + $2 + $2; 
  50. }); 
  51. sParsed = ('YYYY/MM/DD').replace(/YYYY|MM|DD/g, function(m){ 
  52. return (sFormat.indexOf(m) + 1) ?  
  53. sDate.substr(sFormat.indexOf(m), m.length) : ''; 
  54. }); 
  55. if (sParsed.charAt(0) == '/') { 
  56. // if no year specified, assume the current year 
  57. sParsed = (new Date().getFullYear()) + sParsed; 
  58. } 
  59. if (sParsed.charAt(sParsed.length - 1) == '/') { 
  60. // if no date, assume the 1st of the month 
  61. sParsed += '01'; 
  62. } 
  63. // should end up with 10 characters.. 
  64. return ( sParsed.length == 10 ) ? sParsed : ''; 
  65. }; 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选