一、利用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. js 大厦之JavaScript事件

    1.js事件简介 事件(Event) 是 JavaScript 应用跳动的心脏 ,进行交互,使网页动起来.也是把所有东西粘在一起的胶水.当我们与浏览器中 Web 页面进行某些类型的交互时,事件就发生了 ...

  2. 最火移动端跨平台方案盘点:React Native、weex、Flutter

    1.前言 跨平台一直是老生常谈的话题,cordova.ionic.react-native.weex.kotlin-native.flutter等跨平台框架的百花齐放,颇有一股推倒原生开发者的势头. ...

  3. scws简单中文分词

    demo如下: /** * 中文分词 * @param $keyword * @param $getTop * @param $limit * @return array */ function sp ...

  4. hbaes之createTable执行流程

    hbase的客户端代码并不想hive一样用java编写,shell调用,而是使用ruby编写. 在admin.rb文件中方法create,其中接受两个参数,其中第二个参数类型为变长参数. 而在crea ...

  5. [转]MySQL 清空慢查询文件

    概述 本章主要写当慢查询文件很大的时候怎样在线生成一个新的慢查询文件. 测试环境:mysql 5.6.21 步骤 配置慢查询 默认的my.cnf文件在/etc/目录下 vim /etc/my.cnf ...

  6. vue axios 简单封装以及思考

    先安装 axios npm install axios axios的详细介绍以及用法 就不多说了请 移步 github ➡️  https://github.com/axios/axios 下面是简单 ...

  7. visual Studio 2017 扩展开发(二)《菜单图标详解》

    在上一篇我们在菜单栏创建了一个菜单,菜单上显示了一个图标跟文本.那么我们自己创建的菜单如何修改自定义的菜单图标呢.下面娓娓道来..... 首先你要有一个图,创建一个32位的位图.这个位图的像素是16p ...

  8. python+appium的物理按键代码

    代码就一句driver.keyevent()括号内填入的是物理按键的数字代号 代号表: 电话键 KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYCODE_HOM ...

  9. Python快速学习10: 循环的对象及设计 (生活的规律)

    前言 系列文章:[传送门] 生活逐渐规律,按时睡觉.今天写博客,明天补时间看会书.慢慢的时间很珍惜 我很喜欢! 时钟就像个循环体,我们将它融入生活. 正文 循环对象的并不是随着Python的诞生就存在 ...

  10. Chapter 4 Invitations——2

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