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. ...
随机推荐
- 浅谈 Nginx 的内部核心架构设计
一.前言 Nginx---Ngine X,是一款免费的.自由的.开源的.高性能HTTP服务器和反向代理服务器:也是一个IMAP.POP3.SMTP代理服务器:Nginx以其高性能.稳定性.丰富的功能. ...
- Linux 系统下实践 VLAN
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 01 准备环境 ...
- 【code block】局部代码块+构造代码块+静态代码块
1.局部代码块 位置:位于类的方法中 表示方法:{} 作用:控制变量的生命周期,减少内存消耗 demo: public class LocalCode { public static void mai ...
- JSON库的使用研究(二)
Java 中哪个 JSON 库的解析速度是最快的? 这个问题有意义吗?各个JSON库的性能差距不大?呵呵,差距大不大,自己往下看吧! 这个问题我们应该分为以下四个维度进行研究: 1.序列化 2.反序列 ...
- 破解第二课 JMP法
首先,我用录屏大师自制了一个视频,给视频加上密码.任意输入,看到报错信息“密码不对,请重新输入” 第一步 反汇编窗口右键点击“中文搜索引擎”---“智能搜索”,搜索引擎界面再次搜索“不对”,结果如下: ...
- asp.net core 2.0发布到IIS流程及报错解决方案
我这是个新装的服务器,没有安装任何软件. 一.发布流程 1.安装AspNetCoreModule托管模块,同时会自动安装..net core runtime DotNetCore.2.0.8-Wi ...
- ZOJ Problem Set - 3708 Density of Power Network
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3708 #include <stdio.h> #include ...
- Gradle安装使用以及基本操作
这两天看到越来越多的在接触Gradle,然后发现我之前没有做过记录,而且之后下个月的一些有关SpringBoot的东西也需要用到,所以这里就来记录一下,方便以后使用. 简单介绍 Gradle是一个好用 ...
- linux 命令 — lsof
lsof 列出打开的文件 输出 FD: 文件描述符,cwd表示应用程序当前工作目录,txt表示打开的是程序代码(二进制文件或者共享库),0标准输入,1标准输出,2错误流 TYPE:DIR目录,CHR字 ...
- DocumentFragment对象
一般动态创建html元素都是创建好了直接appendChild()上去,但是如果要添加大量的元素还用这个方法的话就会导致大量的重绘以及回流,所以需要一个'缓存区'来保存创建的节点,然后再一次性添加到父 ...