码迷,mamicode.com
首页 > 其他好文 > 详细

输入年份,和这个年份的第几天,转成年月日。

时间:2016-09-27 11:31:08      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

  1 var year = 2016; //年份
  2 var days = 170; //第几天
  3 var reminder = 0;
  4 
  5 function days2month_day(year, days) {
  6     "use strict";
  7     var month = "1月"; //默认1月
  8     var day = "1日";
  9     //闰年 1月31  2月29 3月31 4月30 5月31 6月30 7月31 8月31 9月30 10月31 11月30 12月31
 10     if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
 11 
 12         if (days - 31 <= 0) {
 13             month = "1月";
 14             reminder = 31 - days;
 15             day = "" + reminder + "日";
 16         } else if (days - 60 <= 0) {
 17             month = "2月";
 18             reminder = 29 - (days - 31);
 19             day = "" + reminder + "日";
 20 
 21         } else if (days - 91 <= 0) {
 22             month = "3月";
 23             reminder = 31 - (days - 60);
 24             day = "" + reminder + "日";
 25         } else if (days - 121 <= 0) {
 26             month = "4月";
 27             reminder = 30 - (days - 91);
 28             day = "" + reminder + "日";
 29 
 30         } else if (days - 152 <= 0) {
 31             month = "5月";
 32             reminder = 31 - (days - 121);
 33             day = "" + reminder + "日";
 34 
 35         } else if (days - 182 <= 0) {
 36             month = "6月";
 37             reminder = 30 - (days - 152);
 38             day = "" + reminder + "日";
 39 
 40         } else if (days - 213 <= 0) {
 41             month = "7月";
 42             reminder = 31 - (days - 182);
 43             day = "" + reminder + "日";
 44 
 45         } else if (days - 244 <= 0) {
 46             month = "8月";
 47             reminder = 31 - (days - 213);
 48             day = "" + reminder + "日";
 49 
 50         } else if (days - 274 <= 0) {
 51             month = "9月";
 52             reminder = 30 - (days - 244);
 53             day = "" + reminder + "日";
 54 
 55         } else if (days - 305 <= 0) {
 56             month = "10月";
 57             reminder = 31 - (days - 274);
 58             day = "" + reminder + "日";
 59 
 60         } else if (days - 335 <= 0) {
 61             month = "11月";
 62             reminder = 30 - (days - 305);
 63             day = "" + reminder + "日";
 64 
 65         } else if (days - 366 <= 0) {
 66             month = "12月";
 67             reminder = 31 - (days - 335);
 68             day = "" + reminder + "日";
 69         }
 70 
 71 
 72     }
 73     else {
 74         //平年 2月28天
 75         if (days - 31 <= 0) {
 76             month = "1月";
 77             reminder = 31 - days;
 78             day = "" + reminder + "日";
 79         } else if (days - 59 <= 0) {
 80             month = "2月";
 81             reminder = 28 - (days - 31);
 82             day = "" + reminder + "日";
 83 
 84         } else if (days - 90 <= 0) {
 85             month = "3月";
 86             reminder = 31 - (days - 59);
 87             day = "" + reminder + "日";
 88         } else if (days - 120 <= 0) {
 89             month = "4月";
 90             reminder = 30 - (days - 90);
 91             day = "" + reminder + "日";
 92 
 93         } else if (days - 151 <= 0) {
 94             month = "5月";
 95             reminder = 31 - (days - 120);
 96             day = "" + reminder + "日";
 97 
 98         } else if (days - 181 <= 0) {
 99             month = "6月";
100             reminder = 30 - (days - 151);
101             day = "" + reminder + "日";
102 
103         } else if (days - 212 <= 0) {
104             month = "7月";
105             reminder = 31 - (days - 181);
106             day = "" + reminder + "日";
107 
108         } else if (days - 243 <= 0) {
109             month = "8月";
110             reminder = 31 - (days - 212);
111             day = "" + reminder + "日";
112 
113         } else if (days - 273 <= 0) {
114             month = "9月";
115             reminder = 30 - (days - 243);
116             day = "" + reminder + "日";
117 
118         } else if (days - 304 <= 0) {
119             month = "10月";
120             reminder = 31 - (days - 273);
121             day = "" + reminder + "日";
122 
123         } else if (days - 334 <= 0) {
124             month = "11月";
125             reminder = 30 - (days - 304);
126             day = "" + reminder + "日";
127 
128         } else if (days - 365 <= 0) {
129             month = "12月";
130             reminder = 31 - (days - 334);
131             day = "" + reminder + "日";
132         }
133 
134 
135     }
136     return "" + year + "年" + month + day;
137 }
138 
139 var year_month_day = days2month_day(2016, 170);
140 console.log(year_month_day);

 

输入年份,和这个年份的第几天,转成年月日。

标签:

原文地址:http://www.cnblogs.com/huenchao/p/5912043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!