Angular6 项目开发常用时间组件服务
一、利用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 项目开发常用时间组件服务的更多相关文章
- iOS开发——实用篇Swift篇&项目开发常用实用技术
项目开发常用实用技术 实现拨打电话 要实现打电话功能,最简单最直接的方式便是:直接跳到拨号界面 (注意:这个需要真机调试,模拟器无效果) UIApplication.sharedApplica ...
- iOS项目开发常用功能静态库
YHDeveloperTools iOS项目开发常用功能静态库 查看源码 功能方法: 1.字符检查 [NSString checkStringWithType:Email andTargetStrin ...
- [Openwrt 项目开发笔记]:Samba服务&vsFTP服务(四)
[Openwrt项目开发笔记]系列文章传送门:http://www.cnblogs.com/double-win/p/3888399.html 正文: 在上一节中,我们讲述了如何在路由器上挂载U盘,以 ...
- Angular 项目开发中父子组件传参
在项目开发中经常会遇到 组件之间传参的问题.今天总结下在使用angular的项目中父子组件传参的问题: 1.父组件向子组件传参: 然后在父组件中 然后在父组件的html中 然后就可以在子组件中使用了 ...
- spring boot 项目开发常用目录结构
在spring boot开发中一些常用的目录划分 转载自https://blog.csdn.net/Auntvt/article/details/80381756: 一.代码层结构 根目录:net.c ...
- day 17 项目开发常用模块
---恢复内容开始--- time模块 import time print(time.time()) # 时间戳: print(time.strftime("%Y-%m-%d %X" ...
- 前端项目中常用es6知识总结 -- Async、Await让异步美如画
项目开发中一些常用的es6知识,主要是为以后分享小程序开发.node+koa项目开发以及vueSSR(vue服务端渲染)做个前置铺垫. 项目开发常用es6介绍 1.块级作用域 let const 2. ...
- 前端项目中常用es6知识总结 -- 箭头函数及this指向、尾调用优化
项目开发中一些常用的es6知识,主要是为以后分享小程序开发.node+koa项目开发以及vueSSR(vue服务端渲染)做个前置铺垫. 项目开发常用es6介绍 1.块级作用域 let const 2. ...
- 前端项目中常用es6知识总结 -- Promise逃脱回调地狱
项目开发中一些常用的es6知识,主要是为以后分享小程序开发.node+koa项目开发以及vueSSR(vue服务端渲染)做个前置铺垫. 项目开发常用es6介绍 1.块级作用域 let const 2. ...
随机推荐
- 还在使用SimpleDateFormat?
阅读本文大概需要 3.2 分钟. 前言 日常开发中,我们经常需要使用时间相关类,想必大家对SimpleDateFormat并不陌生.主要是用它进行时间的格式化输出和解析,挺方便快捷的,但是Simple ...
- springmvc的Controller里实现转发的同时弹出提示对话框
以前写servlet时就用到这个,但是现在学了springMVC+hibernate后就不知道怎么实现了,后来在网上找了好多,看了好多,最后经过自己实践成功的如下: 1.首先是Controller控制 ...
- CoCos2dx开发:中文乱码
一.FontToUTF8()方法修改字体: 1.HelloWorldScene.h头文件中声明方法: char* FontToUTF8(const char* font); 2.HelloWorldS ...
- Python中使用PyCharm为函数及参数增加注释
在函数名中键入数遍光标,左上角亮起小灯泡,点击小灯泡 选中第二行内容 在"""后添加函数注释,以及参数注释 然后再引用函数时,选中函数,Ctrl q 即可显示函数以及参 ...
- CSS Modules 与 scoped 的不一样
What ? css 的作用域表现. Css modules 是一个CSS文件,其中所有类名和动画名称默认为局部作用域. 使用JS编译原生的CSS文件,使其具备模块化的能力,该文件需要import使用 ...
- netty中的传输
终于在课设的闲时间把netty实战的四五章给解决了 这里来记录一下第四章里面所讲的IO 首先说到IO,我想,必须要先了解阻塞,非阻塞,同步和异步这四个词 看到一个讲的很易懂的例子:https://ww ...
- 原生js的简单倒计时
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>倒计 ...
- AutoCloseable的用法
今天学习JDBC的时候查看了接口Statement的executeQuery()方法的时候偶然发现了一个新的接口: 看到红笔圈出的那句话当时没反应过来,才知道是接口的多继承,但是后面的那个接口并没有见 ...
- tensorflow 1.0 学习:池化层(pooling)和全连接层(dense)
池化层定义在 tensorflow/python/layers/pooling.py. 有最大值池化和均值池化. 1.tf.layers.max_pooling2d max_pooling2d( in ...
- leetcode — minimum-window-substring
import java.util.HashMap; import java.util.Map; /** * * Source : https://oj.leetcode.com/problems/mi ...