[英]Date and Time function formatting
I want to create a new function named dateWriter with three basic parameters that is "year", "month" and "day" which eventually returns the new date.我想创建一个名为dateWriter的新函数,它具有三个基本参数,即“年”、“月”和“日”,最终返回新日期。
Here is what I have:这是我所拥有的:
function dateWriter(year, month, day) { return; } console.log(dateWriter(2020, 1, 25));
I'm not at all sure what to return inside the body, or how to get it into date format.我完全不确定要在体内返回什么,或者如何将其转换为日期格式。
Thanks!谢谢!
function dateWriter(year, month, day) { return day+"/"+month+"/"+year; } console.log(dateWriter(2020, 1, 25));
You can use new Date(year, month, date)
constructor to create date object.您可以使用
new Date(year, month, date)
构造函数来创建日期对象。 Since integer value representing the month, beginning with 0
for January to 11
for December.由于表示月份的整数值,从
0
开始表示一月到11
表示十二月。 Hence you need to subtract 1
from your month
.因此,您需要从您的
month
减去1
。
function dateWriter(year, month, day) { return new Date(year, month-1, day);; } console.log(dateWriter(2020, 1, 25));
The following will create a new date object with passed in values and return the same.以下将创建一个具有传入值的新日期对象并返回相同的值。
function dateWriter(year, month, day) {
return new Date(year, month-1, day);
}
console.log(dateWriter(2020, 1, 25));
If you would like to read more about the Date object in JS, the following link will help you : https://www.w3schools.com/js/js_dates.asp如果您想了解有关 JS 中 Date 对象的更多信息,以下链接将对您有所帮助: https : //www.w3schools.com/js/js_dates.asp
So what it looks like your question is asking is for you to write a function that returns a date object from parameters.所以看起来你的问题是让你写一个函数,从参数返回一个日期对象。 Lucky for you, that is pretty easy!
幸运的是,这很容易!
In JavaScript, when creating an object from a constructor function, you use the “new” keyword.在 JavaScript 中,从构造函数创建对象时,使用“new”关键字。 The date object is one such object created like that, so you could write
日期对象就是这样创建的对象之一,因此您可以编写
return new Date(year, month-1, day)
In the body of the function to return a new date object, constructed with your parameters在函数体中返回一个新的日期对象,用你的参数构造
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.