static getNowDate(): string {
const date = new Date();
let month: string | number = date.getMonth() + 1;
let strDate: string | number = date.getDate();
if (month <= 9) {
month = "0" + month;
if (strDate <= 9) {
strDate = "0" + strDate;
return date.getFullYear() + "-" + month + "-" + strDate + " "
+ date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
3:日期工具类:
dateTime.ts:
formatDate(){
//三目运算符
const Dates = new Date();
const Year : number = Dates.getFullYear();
//月份下标是0-11
const Months : any = ( Dates.getMonth() + 1 ) < 10 ? '0' + (Dates.getMonth() + 1) : ( Dates.getMonth() + 1);
//具体的天数
const Day : any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
const Hours = Dates.getHours() < 10 ? '0' + Dates.getHours() : Dates.getHours();
const Minutes = Dates.getMinutes() < 10 ? '0' + Dates.getMinutes() : Dates.getMinutes();
const Seconds = Dates.getSeconds() < 10 ? '0' + Dates.getSeconds() : Dates.getSeconds();
//返回数据格式
return Year + '-' + Months + '-' + Day + '-' + Hours + ':' + Minutes + ':' + Seconds;
扩展链接:
Cocos Creator制作倒计时显示的优化
TypeScript 获取当前日期及前后范围日期【Array】
1:TypeScript 获取时间戳:Date.parse(new Date().tostring()); 2:TypeScript获取格式化日期:static getNowDate(): string { const date = new Date(); let month: string | number = date.getMonth() + 1; let s...
-r, --replace replace .ts file
--verify checking file format
--baseDir < path> config file lookup from < path>
--stdin get fo
SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm:ss"); //造型随你喜欢
Long time=null;
//timestamp为需要转换的时间戳
time=new Long(String.valueOf(timestamp));
//转换成字符串
String timestr=format.format
const opt = {
"Y+": date.getFullYear().toString(), // 年
"m+": (date.getMonth() + 1).toString(), // 月
"d+": date.getDate().toString(), // 日
"H+": date.getHours..
在传统的js语法中,所有数据类型直接用var a=xxx,typescript中,为了使编写的代码更规范,更有利于维护,增加了类型校验,跟java一样,一个变量定义以后,你给他赋值必须是对应的数据类型,不然的话就会报错。在typescript中主要给我们提供了以下数据类型
布尔类型(boolean)
数字类型(number)
字符串类型(string)
数组类型(array)
元组类型(tuple)
枚举类型(enum)
任意类型(any)
null 和 undefined
void类型
.neve
在TypeScript中,可以使用内置的`Date`对象来处理日期和时间。要格式化日期,可以使用`toLocaleString()`方法并传递所需的语言和选项。以下是一个示例:
```typescript
const date = new Date();
const formattedDate = date.toLocaleString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
console.log(formattedDate);
这将输出类似于`October 1, 2022, 10:30:15 AM`的格式化日期字符串。你可以根据需要调整语言和选项。