数字日期转化为汉字日期格式
- 格式:doc
- 大小:43.00 KB
- 文档页数:5
数字日期转化为汉字日期格式
1.
2.
3. /* 功能:YYYY-MM-DD 数字日期转化为汉字
4. 例:1984-3-7 ->一九八四年三月七日
5. 调用:baodate2chinese("1984-3-7")
6. */
7. var chinese = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
8. var len = ['十'];
9. var ydm = ['年', '月', '日'];
10. function num2chinese(s) {
11. //将单个数字转成中文.
12. s = "" + s;
13. slen = s.length;
14. var result = "";
15. for (var i = 0; i 16. result += chinese[s.charAt(i)]; 17. } 18. return result; 19. } 20. 21. function n2c(s) { 22. //对特殊情况进行处理. 23. s = "" + s; 24. var result = ""; 25. if (s.length == 2) { 26. if (s.charAt(0) == "1") { 27. if (s.charAt(1) == "0") return len[0]; 28. return len[0] + chinese[s.charAt(1)]; 29. } 30. if (s.charAt(1) == "0") return chinese[s.charAt(0)] + len[0]; 31. return chinese[s.charAt(0)] + len[0] + chinese[s.charAt(1)]; 32. } 33. return num2chinese(s) 34. } 35. 36. function baodate2chinese(s) { 37. //验证输入的日期格式.并提取相关数字. 38. var datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})(\2)(\d{1,2})$/; 39. var matchArray = s.match(datePat); 40. var ok = ""; 41. if (matchArray == null) return false; 42. for (var i = 1; i 43. ok += n2c(matchArray[i] - 0) + ydm[(i - 1) / 2]; 44. } 45. return ok; 46. } 47. 48. 49. 50. YYYY-MM-DD: 51. 52. 中文显示: 53. 54. 55. value="转换"> 56. 57. C# 1. /// 2. /// 数字日期转换成中文日期 3. /// 4. /// 日期 5. /// 6. public string changeDate(string strDate) 7. { 8. char[] strChinese; 9. strChinese = new char[] { '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十 ' }; 10. StringBuilder result = new StringBuilder(); 11. // 依据正则表达式判断参数是否正确 12. Regex theReg = new Regex(@"(d{2}|d{4})(/|-)(d{1,2})(/|-)(d{1,2})"); 13. //if (theReg.Match(strDate).Length != 0) 14. //{ 15. // 将数字日期的年月日存到字符数组str中 16. string[] str = null; 17. if (strDate.Contains("-")) 18. { 19. str = strDate.Split('-'); 20. } 21. else if (strDate.Contains("/")) 22. { 23. str = strDate.Split('/'); 24. } 25. 26. // str[0]中为年,将其各个字符转换为相应的汉字 27. for (int i = 0; i < str[0].Length; i++) 28. { 29. result.Append(strChinese[int.Parse(str[0][i].ToString())]); 30. } 31. result.Append("年"); 32. 33. // 转换月 34. int month = int.Parse(str[1]); 35. int MN1 = month / 10; 36. int MN2 = month % 10; 37. 38. if (MN1 > 1)