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

Posted 暗影侠客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了输入年份,和这个年份的第几天,转成年月日。相关的知识,希望对你有一定的参考价值。

  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);

 

以上是关于输入年份,和这个年份的第几天,转成年月日。的主要内容,如果未能解决你的问题,请参考以下文章

Python根据年月日,计算是一年的第几天

C语言输入年月日,判断第几天的问题最后计算是第几天出错了

Java语言,已知一个年月日的日期,获取该日期是这一年的多少天...........

C语言,求年月日

输入年份,月份,日期计算该日期是一年中的第几天//雪碧和可乐temp

用java运行给出年份和是这一年的第几天,求是这一年的几月几号急等拜托各位大神