TypeScript에서 날짜 유형을 어떻게 표현합니까?
TypeScript에서 날짜를 어떻게 표현합니까?날짜는 TypeScript 유형이 아니므로any또는object올바른 방법이 있을 것 같습니다.
let myDate: any = new Date();
너무 간단한 질문임에도 불구하고 구글에서는 많은 것을 찾을 수 없었다.
종류는Date:
const d: Date = new Date(); // but the type can also be inferred from "new Date()" already
이는 다른 모든 오브젝트인스턴스와 동일합니다.
TypeScript에서는 모든 클래스 또는 인터페이스를 유형으로 사용할 수 있습니다.
const date = new Date();
에 대해서는 이미 알고 있습니다.date라고 정의를 입력하다Date는 DateConstructor 인터페이스에서 참조되는 내부 TypeScript 객체입니다.
그리고 사용한 컨스트럭터의 정의는 다음과 같습니다.
interface DateConstructor {
new(): Date;
...
}
알기 쉽게 하기 위해 다음을 사용할 수 있습니다.
const date: Date = new Date();
유형 정의가 누락되어 있을 수 있습니다.Date예를 들어 ES6 lib와tsconfig.json정의:
"compilerOptions": {
"target": "ES6",
"lib": [
"es6",
"dom"
],
원하는 버전의 JavaScript를 대상으로 이러한 설정을 조정할 수 있습니다.
날짜는 의 인터페이스입니다.
/** Enables basic storage and retrieval of dates and times. */
interface Date {
/** Returns a string representation of a date. The format of the string depends on the locale. */
toString(): string;
/** Returns a date as a string value. */
toDateString(): string;
/** Returns a time as a string value. */
toTimeString(): string;
/** Returns a value as a string value appropriate to the host environment's current locale. */
toLocaleString(): string;
/** Returns a date as a string value appropriate to the host environment's current locale. */
toLocaleDateString(): string;
/** Returns a time as a string value appropriate to the host environment's current locale. */
toLocaleTimeString(): string;
/** Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC. */
valueOf(): number;
/** Gets the time value in milliseconds. */
getTime(): number;
/** Gets the year, using local time. */
getFullYear(): number;
/** Gets the year using Universal Coordinated Time (UTC). */
getUTCFullYear(): number;
/** Gets the month, using local time. */
getMonth(): number;
/** Gets the month of a Date object using Universal Coordinated Time (UTC). */
getUTCMonth(): number;
/** Gets the day-of-the-month, using local time. */
getDate(): number;
/** Gets the day-of-the-month, using Universal Coordinated Time (UTC). */
getUTCDate(): number;
/** Gets the day of the week, using local time. */
getDay(): number;
/** Gets the day of the week using Universal Coordinated Time (UTC). */
getUTCDay(): number;
/** Gets the hours in a date, using local time. */
getHours(): number;
/** Gets the hours value in a Date object using Universal Coordinated Time (UTC). */
getUTCHours(): number;
/** Gets the minutes of a Date object, using local time. */
getMinutes(): number;
/** Gets the minutes of a Date object using Universal Coordinated Time (UTC). */
getUTCMinutes(): number;
/** Gets the seconds of a Date object, using local time. */
getSeconds(): number;
/** Gets the seconds of a Date object using Universal Coordinated Time (UTC). */
getUTCSeconds(): number;
/** Gets the milliseconds of a Date, using local time. */
getMilliseconds(): number;
/** Gets the milliseconds of a Date object using Universal Coordinated Time (UTC). */
getUTCMilliseconds(): number;
/** Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). */
getTimezoneOffset(): number;
/**
* Sets the date and time value in the Date object.
* @param time A numeric value representing the number of elapsed milliseconds since midnight, January 1, 1970 GMT.
*/
setTime(time: number): number;
/**
* Sets the milliseconds value in the Date object using local time.
* @param ms A numeric value equal to the millisecond value.
*/
setMilliseconds(ms: number): number;
/**
* Sets the milliseconds value in the Date object using Universal Coordinated Time (UTC).
* @param ms A numeric value equal to the millisecond value.
*/
setUTCMilliseconds(ms: number): number;
/**
* Sets the seconds value in the Date object using local time.
* @param sec A numeric value equal to the seconds value.
* @param ms A numeric value equal to the milliseconds value.
*/
setSeconds(sec: number, ms?: number): number;
/**
* Sets the seconds value in the Date object using Universal Coordinated Time (UTC).
* @param sec A numeric value equal to the seconds value.
* @param ms A numeric value equal to the milliseconds value.
*/
setUTCSeconds(sec: number, ms?: number): number;
/**
* Sets the minutes value in the Date object using local time.
* @param min A numeric value equal to the minutes value.
* @param sec A numeric value equal to the seconds value.
* @param ms A numeric value equal to the milliseconds value.
*/
setMinutes(min: number, sec?: number, ms?: number): number;
/**
* Sets the minutes value in the Date object using Universal Coordinated Time (UTC).
* @param min A numeric value equal to the minutes value.
* @param sec A numeric value equal to the seconds value.
* @param ms A numeric value equal to the milliseconds value.
*/
setUTCMinutes(min: number, sec?: number, ms?: number): number;
/**
* Sets the hour value in the Date object using local time.
* @param hours A numeric value equal to the hours value.
* @param min A numeric value equal to the minutes value.
* @param sec A numeric value equal to the seconds value.
* @param ms A numeric value equal to the milliseconds value.
*/
setHours(hours: number, min?: number, sec?: number, ms?: number): number;
/**
* Sets the hours value in the Date object using Universal Coordinated Time (UTC).
* @param hours A numeric value equal to the hours value.
* @param min A numeric value equal to the minutes value.
* @param sec A numeric value equal to the seconds value.
* @param ms A numeric value equal to the milliseconds value.
*/
setUTCHours(hours: number, min?: number, sec?: number, ms?: number): number;
/**
* Sets the numeric day-of-the-month value of the Date object using local time.
* @param date A numeric value equal to the day of the month.
*/
setDate(date: number): number;
/**
* Sets the numeric day of the month in the Date object using Universal Coordinated Time (UTC).
* @param date A numeric value equal to the day of the month.
*/
setUTCDate(date: number): number;
/**
* Sets the month value in the Date object using local time.
* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.
* @param date A numeric value representing the day of the month. If this value is not supplied, the value from a call to the getDate method is used.
*/
setMonth(month: number, date?: number): number;
/**
* Sets the month value in the Date object using Universal Coordinated Time (UTC).
* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.
* @param date A numeric value representing the day of the month. If it is not supplied, the value from a call to the getUTCDate method is used.
*/
setUTCMonth(month: number, date?: number): number;
/**
* Sets the year of the Date object using local time.
* @param year A numeric value for the year.
* @param month A zero-based numeric value for the month (0 for January, 11 for December). Must be specified if numDate is specified.
* @param date A numeric value equal for the day of the month.
*/
setFullYear(year: number, month?: number, date?: number): number;
/**
* Sets the year value in the Date object using Universal Coordinated Time (UTC).
* @param year A numeric value equal to the year.
* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively. Must be supplied if numDate is supplied.
* @param date A numeric value equal to the day of the month.
*/
setUTCFullYear(year: number, month?: number, date?: number): number;
/** Returns a date converted to a string using Universal Coordinated Time (UTC). */
toUTCString(): string;
/** Returns a date as a string value in ISO format. */
toISOString(): string;
/** Used by the JSON.stringify method to enable the transformation of an object's data for JavaScript Object Notation (JSON) serialization. */
toJSON(key?: any): string;
}
Typescript는 숫자, 문자열 또는 사용자 지정 유형을 사용하는 경우와 마찬가지로 즉시 날짜 인터페이스를 인식합니다.사용방법:
myDate : Date;
다른 사람들이 언급했듯이 유형은 다음과 같습니다.Date.
이 타입은 컴파일 시에 실제로 이 타입이 존재하는 경우에 편리합니다.Date물건.아래는 (새로운 키워드를 잊어버리고) 자주 발생하는 실수이며 컴파일 시에 검출되는 것을 알 수 있습니다.
const date: Date = new Date();
function logDayNumber(date: Date){
console.log(date.getDay())
}
// Works fine
logDayNumber(date);
// Compile time error which happens when you forget new a String we be produced from Date()
// Argument of type 'string' is not assignable to parameter of type 'Date'
logDayNumber(Date());
언급URL : https://stackoverflow.com/questions/45485073/how-do-i-express-a-date-type-in-typescript
'programing' 카테고리의 다른 글
| Windows 개발 머신을 사용하여 iPhone용으로 개발하려면 어떻게 해야 하나요? (0) | 2023.04.09 |
|---|---|
| Bash에서 파일 이름 및 확장자 추출 (0) | 2023.04.09 |
| 봄은 추상적인 수업 안에서 자동 학습할 수 있나요? (0) | 2023.04.04 |
| AJAX 요청을 정기적으로 실행하는 방법 (0) | 2023.04.04 |
| 모든 범주 나열(손자만 고정됨)(Wordpress) (0) | 2023.04.04 |