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. ...
随机推荐
- ECDSA数字签名算法
一.ECDSA概述 椭圆曲线数字签名算法(ECDSA)是使用椭圆曲线密码(ECC)对数字签名算法(DSA)的模拟.ECDSA于1999年成为ANSI标准,并于2000年成为IEEE和NIST标准.它在 ...
- Javascript高级编程学习笔记(66)—— 事件(10)变动事件
变动事件 DOM2级的变动事件,能在DOM中的一部分发生变化时给出提示 变动事件是为XML或HTML DOM 设计的,并不特定于某种语言 DOM2级定义了如下变动事件: DOMSubtreeModif ...
- Nginx 在 Linux 上的安装和配置
一.Nginx的安装 1.单台Nginx的安装 Nginx在Linux上的安装可以参考这篇博客:http://blog.csdn.net/molingduzun123/article/details/ ...
- Linux 下 pushd,popd,cd- 用法
一,为何要使用这几个命令? 可能大家会有疑问,为何要使用这几个命令, 难道用cd不就可以切换目录了吗? 没错,使用cd就可以切换到需要访问的目录, 但是有时会是一个路径很长,层次很多的目录 ...
- .Net Core新建解决方案,添加项目引用,使用VSCode调试
并不是我自己琢磨的,是看了别人学习的,因为写的都不完整,所以就整理一下记录后面忘了回看. 反正.Net Core是跨平台的,就不说在什么系统上了.假设我要建一个名为Doggie的解决方案,里面包含了一 ...
- iOS----KVC和KVO 详解
一. KVC 1.KVC介绍 KVC 就是键值编码(key-value-coding). 2.KVC 的主要作用: (1)通过键值路径为对象的属性赋值.主要是可以为私有的属性赋值. AppleView ...
- Python——爬虫进阶
课程内容 Python爬虫——反爬 Python加密与解密 Python模块——HashLib与base64 Python爬虫——selenium模块 Python——pytessercat识别 ...
- git push 时发生 error: failed to push some refs to 错误 (解决办法)
出现问题的原因:在github上更新了README.md,没有更新到本地仓库.而在本地git仓库又修改了文件,这时使用 git push origin master 推送到远程仓库后就出现了下面的问题 ...
- lazy_import源码解析(原创)
参考链接: An approach to lazy importing in Python 3.7(这个是参考源) Python3.7中一种懒加载的方式(中文翻译) 原博客核心: 以前的两种惰性/延迟 ...
- Quartz的使用案例
一.介绍 项目中的调度任务可以使用Quartz任务调度框架 1.Job接口:这个接口里面只定义了一个方法,excute void execute(JobExecutionContext context ...