当前位置:网站首页>JS ask for the day of the year
JS ask for the day of the year
2022-06-13 08:34:00 【Just one time】
The user enters the date of the year to calculate the day of the year
Ideas :
1. For the year , You need to judge whether the entered year is a leap year , If it is a leap year and the month is greater than 2 Monthly rule Add one to the total number of days
2. For months , For example, user input 4 month Then the current number of days should be before 3 Add the months and days of the month and then add the entered date .( You can store the days of each month in an array ) Traverse the array and accumulate the number of days .
3. For Japan , If user input 1 month , Then enter the number of days , It is the day of the year .
<script>
/* Ideas : 1 First judge whether the year is a normal year or a leap year ( In ordinary years 2 Monthly 28 God Leap year's 2 Monthly 29 God ) 2 The number of days in each month in a normal year monthDay = [31,28,31,30,31,30,31,31,30,31,30,31] If user input 1 month The number of days is returned directly If the year entered by the user is a leap year and the month entered is greater than 2 Then the total number of days plus 1 If user input 3 month 30 Japan be Traversal array monthDay The judgment condition is i < month - 1 num = day + monthDay[i] */
var year = Number(prompt(' Please enter the current year '));
var month = Number(prompt(' Please enter ' + year + ' The month in '));
var day = Number(prompt(' Please enter ' + year + ' year ' + month + ' The date of the month '));
// Determine whether the year is a leap year
function isfullyear(year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
}
}
alert(' At present, it is '+ year + ' In the middle of the year ' + getDays(year,month,day) + ' God ');
// Defined function Put the user input year , month , Japan Pass as an argument to a function
function getDays(year, month, day) {
// Assign days to num
var num = day;
// If user input 1 month The number of days is returned directly
if (month == 1) {
return day;
}
var monthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Cycle through the months The cycle condition is the month before the user enters the month
for (var i = 0; i < month - 1; i++) {
//num = Days of the month + The number of days in the previous month
num = num + monthDay[i]
}
// If the year entered by the user is a leap year and the month entered is greater than 2 Then the total number of days plus 1
if (isfullyear(year) && month > 2) {
num++;
}
return num;
}
</script>
边栏推荐
- Namespace in TS (1)
- The method of SolidWorks modifying text font in engineering drawing
- DNS domain name resolution service
- Four ways to install MySQL in Linux
- Batch package and download Alibaba OSS files
- JS gets the first month of the year, the second month to the last month, and the first day to the last day
- Methods of importing and exporting settings in Altium Designer
- HCIP_ MGRE comprehensive experiment
- How app inventor accesses resource files in assets directory
- Dest0g3 520迎新賽
猜你喜欢
容器概念和云原生
【Pycharm踩坑记录】配置远程运行后提示,[Errno 2] No such file or directory
How to efficiently manage commodities and inventory in the beverage wholesale industry
Is signed or unsigned selected to create an integer field in MySQL? The answer is as follows:
Buuctf web (VII)
关于redis使用分布式锁的封装工具类
AcWing 1977. Information relay (base ring tree, parallel search set)
Several precise order receiving methods suitable for fresh food wholesale industry
Document contains question type
[notes] like the solution to the problem of slow query (index + explicitly specifying query fields)
随机推荐
In order to resist the flood, the soldiers have been fighting for 89 hours. How many days and hours are there in total?
Microservice Project Construction II: database design
容器概念和云原生
水仙花升级版(自幂数)
Dest0g3 520迎新賽
使用kvm创建三台能通局域网的虚拟机
微服务项目搭建三:自动生成代码
【Pycharm踩坑记录】配置远程运行后提示,[Errno 2] No such file or directory
[virt-manager]远程管理启动虚拟机时,鼠标飘逸的问题
How to hide tabs in nailing applet
Live broadcast review | bas technology innovation exploration under active defense system
How app inventor accesses resource files in assets directory
Do not update the sub component page of parameter object passed from parent to child of nailing applet?
微服务项目搭建二:数据库设计
Three methods to make the scroll bar of div automatically scroll to the bottom
杨氏矩阵查找数字是否存在
Several precise order receiving methods suitable for fresh food wholesale industry
sizeof、strlen求字符长度
How to use annotations in word
Four ways to install MySQL in Linux