一、利用Angular 命令行工具生成一个服务。

  详情见:《Angular环境搭建》,服务代码如下:

 import { Injectable } from '@angular/core';

 @Injectable({
providedIn: 'root'
})
export class TimeutilProvider { private today: Date;
private year: String;
private mouth: String;
private date: String;
private hours: String;
private minutes: String;
private seconds: String;
private milliseconds: String;
private week:number;
private weekday:Array<string> =["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
constructor () {
this.today = new Date();
this.year = this.today.getFullYear().toString();
this.mouth = (this.today.getMonth() + 1).toString();
this.date = this.today.getDate().toString();
this.hours = this.today.getHours().toString();
this.minutes = this.today.getMinutes().toString();
this.seconds = this.today.getSeconds().toString();
this.milliseconds = this.today.getMilliseconds().toString(); this.week = this.today.getDay(); if (Number(this.mouth) < 10) {
this.mouth = '0' + this.mouth;
} else {
}
if (Number(this.date) < 10) {
this.date = '0' + this.date;
} else {
}
if (Number(this.hours) < 10) {
this.hours = '0' + this.hours;
} else {
}
if (Number(this.minutes) < 10) {
this.minutes = '0' + this.minutes;
} else {
}
if (Number(this.seconds) < 10) {
this.seconds = '0' + this.seconds;
}
if (10 > Number(this.milliseconds)) {
this.milliseconds = '00' + this.milliseconds;
} else if ((10 <= Number(this.milliseconds)) && (100 > Number(this.milliseconds))) {
this.milliseconds = '0' + this.milliseconds;
} else {
// dothing
}
} //返回当前星期
currentWeek(){
return this.weekday[this.week].toString();
// this.week.toString();
} //20190306115830234格式
currentTime() {
return this.year + '' + this.mouth + '' + this.date + '' + this.hours + '' + this.minutes + '' + this.seconds + '' + this.milliseconds;
} //一般用于生成唯一标识,例如uuid
currentTime_id() {
// 三位随机数以免重复
const random = (Math.random() * 100000000000000000).toString().substr(0, 3); return this.year + '' + this.mouth + '' + this.date + ''
+ this.hours + '' + this.minutes + '' + this.seconds + '' + this.milliseconds + random;
} //2019-03-06 11:59格式
currentTime_datestyle() {
return this.year + '-' + this.mouth + '-' + this.date + ' ' + this.hours + ':' + this.minutes + '';
} //2019-03-06 11:59:30格式
currentTime_datesecondsstyle() {
return this.year + '-' + this.mouth + '-' + this.date + ' ' + this.hours + ':' + this.minutes + ':' + this.seconds;
} // currentTime_date(){
// return this.year + '-' + this.mouth + '-' + this.date;
// } // 返回当前时间"10:25:34"格式
currentTime_time(){
let now=new Date();
let year = (now.getFullYear()).toString();
let month = (now.getMonth()+1).toString();
let date = (now.getDate()).toString();
let hour = (now.getHours()).toString();
let minute= (now.getMinutes()).toString();
let second= (now.getSeconds()).toString(); if (Number(month) < 10) {
month = '0' + month;
} else {
}
if (Number(date) < 10) {
date = '0' + date;
} else {
}
if (Number(hour) < 10) {
hour = '0' + hour;
} else {
}
if (Number(minute) < 10) {
minute = '0' + minute;
} else {
}
if (Number(second) < 10) {
second = '0' + second;
} return hour + ':' + minute + ':' + second;
} //将当前时间转化为消息类型时间,2019.03.06 11:56:30格式
convertToDate_msgTimestamp(nows){
let now=new Date(nows);
let year = (now.getFullYear()).toString();
let month = (now.getMonth()+1).toString();
let date = (now.getDate()).toString();
let hour = (now.getHours()).toString();
let minute= (now.getMinutes()).toString();
let second= (now.getSeconds()).toString(); if (Number(month) < 10) {
month = '0' + month;
} else {
}
if (Number(date) < 10) {
date = '0' + date;
} else {
}
if (Number(hour) < 10) {
hour = '0' + hour;
} else {
}
if (Number(minute) < 10) {
minute = '0' + minute;
} else {
}
if (Number(second) < 10) {
second = '0' + second;
}
return year+"."+month+"."+date+" "+hour+":"+minute+":"+second;
} //2019-03-06 11:56:30格式
convertToDate_timestamp(nows){
var now=new Date(nows);
var year = (now.getFullYear()).toString();
var month = (now.getMonth()+1).toString();
var date = (now.getDate()).toString();
var hour = (now.getHours()).toString();
var minute= (now.getMinutes()).toString();
var second= (now.getSeconds()).toString(); if (Number(month) < 10) {
month = '0' + month;
} else {
}
if (Number(date) < 10) {
date = '0' + date;
} else {
}
if (Number(hour) < 10) {
hour = '0' + hour;
} else {
}
if (Number(minute) < 10) {
minute = '0' + minute;
} else {
}
if (Number(second) < 10) {
second = '0' + second;
}
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
} //将消息时间格式转化为时间戳格式
convertTotimestamp(date:string){
return date.replace(".","-");
} //将消息时间格式转化为Date类型
convertMsgtimeToDate(date:string){
//date = "2018.11.01 10:00:00";
date.replace(".","-");
return new Date(date)
} //将时间戳格式日期转化为Date类型
convertTimestampToDate(date:string){
return new Date(date)
} //获取当前时间前一天时间
getYestardayTime(){
return new Date(new Date().getTime() - 24*60*60*1000);
} //返回当前日期“2018-02-03”格式
currentTime_daystyle() {
return this.year + "-" + this.mouth + "-" + this.date + " ";
} //返回当前时间前一天时间“2018.02.03”格式
currentPreviousTime_daystyle() {
let previousDate = new Date(new Date().getTime() - 24*60*60*1000);
var year = (previousDate.getFullYear()).toString();
var month = (previousDate.getMonth()+1).toString();
var date = (previousDate.getDate()).toString();
if (Number(month) < 10) {
month = '0' + month;
} else {
}
if (Number(date) < 10) {
date = '0' + date;
} else {
} return year + "." + month + "." + date + " ";
}
}

二、时间服务的使用

  1.在要是用的组建构造函数中依赖注入,如下图: 

 constructor(private timeutil : TimeutilProvider) {
//调用方法
console.log("id:",this.timeutil.currentTime_id(););
}

Angular6 项目开发常用时间组件服务的更多相关文章

  1. iOS开发——实用篇Swift篇&项目开发常用实用技术

    项目开发常用实用技术 实现拨打电话 要实现打电话功能,最简单最直接的方式便是:直接跳到拨号界面 (注意:这个需要真机调试,模拟器无效果)     UIApplication.sharedApplica ...

  2. iOS项目开发常用功能静态库

    YHDeveloperTools iOS项目开发常用功能静态库 查看源码 功能方法: 1.字符检查 [NSString checkStringWithType:Email andTargetStrin ...

  3. [Openwrt 项目开发笔记]:Samba服务&vsFTP服务(四)

    [Openwrt项目开发笔记]系列文章传送门:http://www.cnblogs.com/double-win/p/3888399.html 正文: 在上一节中,我们讲述了如何在路由器上挂载U盘,以 ...

  4. Angular 项目开发中父子组件传参

    在项目开发中经常会遇到 组件之间传参的问题.今天总结下在使用angular的项目中父子组件传参的问题: 1.父组件向子组件传参: 然后在父组件中 然后在父组件的html中 然后就可以在子组件中使用了 ...

  5. spring boot 项目开发常用目录结构

    在spring boot开发中一些常用的目录划分 转载自https://blog.csdn.net/Auntvt/article/details/80381756: 一.代码层结构 根目录:net.c ...

  6. day 17 项目开发常用模块

    ---恢复内容开始--- time模块 import time print(time.time()) # 时间戳: print(time.strftime("%Y-%m-%d %X" ...

  7. 前端项目中常用es6知识总结 -- Async、Await让异步美如画

    项目开发中一些常用的es6知识,主要是为以后分享小程序开发.node+koa项目开发以及vueSSR(vue服务端渲染)做个前置铺垫. 项目开发常用es6介绍 1.块级作用域 let const 2. ...

  8. 前端项目中常用es6知识总结 -- 箭头函数及this指向、尾调用优化

    项目开发中一些常用的es6知识,主要是为以后分享小程序开发.node+koa项目开发以及vueSSR(vue服务端渲染)做个前置铺垫. 项目开发常用es6介绍 1.块级作用域 let const 2. ...

  9. 前端项目中常用es6知识总结 -- Promise逃脱回调地狱

    项目开发中一些常用的es6知识,主要是为以后分享小程序开发.node+koa项目开发以及vueSSR(vue服务端渲染)做个前置铺垫. 项目开发常用es6介绍 1.块级作用域 let const 2. ...

随机推荐

  1. MyBatis 的 XML 映射文件使用说明

    简介 文档参考地址:http://www.mybatis.org/mybatis-3/zh/index.html MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器 ...

  2. LabVIEW(三):定时与触发

    一.定时 多功能数据采集板卡的时钟特性,举例为M系列定时引擎:板卡上控制采集和波形发生的三个时钟:AI Sample Clock.AI Convert Clock.AO Sample Clock.所有 ...

  3. Connect By

    connect by 用于存在父子,祖孙,上下级等层级关系的数据表进行层级查询. 语法格式: { CONNECT BY [ NOCYCLE ] condition [AND condition]... ...

  4. foreach加循环体与不加循环体的区别

    案例A(不加{}): <?php function genTree5($items) { foreach ($items as $item) echo $item['id'];die; $ite ...

  5. Mac下快速搭建PHP开发环境

    最近做了一个后端的项目,是用PHP+MySQL+Nginx做的,所以把搭建环境的方法简单总结一下. 备注: 物料:Apache/Nginx+PHP+MySQL+MAMP Mac OS 10.12.1 ...

  6. 全网最详细的Windows里Anaconda-Navigator启动后闪退的解决方案(图文详解)

    不多说,直接上干货!  问题详情 点击 出现Anaconda-Navigator启动后闪退的现象. 或者 装过一次anaconda,貌似按了一个更新的键就打不开了.navigator这个打不开,会停留 ...

  7. 一个BAT老程序员的忠告!

      一.在中国,你千万不要因为学习技术就可以换来稳定的生活和高的薪水待遇,你更不要认为那些从事市场.运营的人,没有前途. 不清楚你是不是知道,咱们中国有相当大的一部分软件公司,他们的软件开发团队都小的 ...

  8. Chapter 4 Invitations——2

    To my dismay, I found myself the center of attention for the rest of that week. 令我沮丧的是, 我发现我自己剩余注意力的 ...

  9. linux 命令 — tr

    tr 对stdin字符进行替换.删除和压缩,基本形式 tr [options] set1 set2 将输入的字符串中的set1字符转换为set2中对应位置的字符 set1.set2表示字符集,如果se ...

  10. 基于redis的分布式锁实现

    1.分布式锁介绍 在计算机系统中,锁作为一种控制并发的机制无处不在. 单机环境下,操作系统能够在进程或线程之间通过本地的锁来控制并发程序的行为.而在如今的大型复杂系统中,通常采用的是分布式架构提供服务 ...