前言

使用的是Moment.js 插件,插件的安装详情请参考官方网址(https://momentjs.com/)

正文

步骤一:引用import * as moment from 'moment';

步骤二:

1.(TOMO):获取十天前的时间

moment().subtract(10, 'days').toDate();   // 06/30 2018
moment().subtract(10, 'days').calendar();  // 06/30 2018
 
 
2.(TOMO): 时间的格式转换
 
moment().format('LLL');  // July 9, 2018 11:03 PM
moment().format('MMMM Do YYYY, h:mm:ss a');  // July 9th 2018, 11:05:39 pm
 
3(TOMO) : 根据以前的时间计算到现在的时间的时长
例如:现在的时间为2018/7/22 16:45,  以前的时间为2018/7/21 16:37

moment('2018/7/21 16:37').startOf('day').fromNow();   // 计算出来的是指为:2 days ago
moment('2018/7/21 16:37').endOf('day').fromNow();   // 计算出来的时间为:17 hours ago

讲解:endof() 是从 2018/7/21 的24点开始计算的,也就是2018/7/22凌晨开始计算的

           startof() 是从2018/7/20 的24点开始计算的,也就是2018/7/21凌晨开始计算的

以上例中时间为例

moment('2018/7/21 16:37').startOf('year').fromNow();   //结果:7 months ago

moment('2018/7/21 16:37').startOf('day').fromNow();   // 结果:2 days ago

moment('2018/7/21 16:37').startOf('hour').fromNow();  // 结果:a day ago

讲解:startof('year')是从2018年开始,七个月

            startof('day')是从2018/7/21 凌晨开始计算

            startof('hour') 是从 2018/7/21 16点开始计算

以上例中时间为例

moment('2018/7/21 16:37').startOf('year').fromNow();   //结果: 7 months ago

moment('2018/7/21 16:37').endOf('year').fromNow();     //结果: in 5 months

 
后记
如有想使用moment.js 插件,具体的使用方法还请参考官方文档,希望对你有帮助。网址:(https://momentjs.com/)
 

关于Angular中时间戳的计算的更多相关文章

  1. angular源码分析:angular中脏活累活的承担者之$interpolate

    一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...

  2. angular源码分析:angular中各种常用函数,比较省代码的各种小技巧

    angular的工具函数 在angular的API文档中,在最前面就是讲的就是angular的工具函数,下面列出来 angular.bind //用户将函数和对象绑定在一起,返回一个新的函数 angu ...

  3. Angular实现简单数据计算与删除

    AngularJS 1)什么是AngularJS AngularJS 简介 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. ...

  4. Angular中ngCookies模块介绍

    1.Cookie介绍 Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短 ...

  5. --关于null在oracle数据库中是否参与计算,进行验证,

    --关于null在oracle数据库中是否参与计算,进行验证,with td as (select null id,1 name from dual ),td1 as ( select null id ...

  6. angular 中父元素ng-repeat后子元素ng-click失效

    在angular中使用ng-repeat后ng-click失效,今天在这个上面踩坑了.特此记录一下. 因为ng-repeat创造了新的SCOPE.如果要使用这个scope的话就必须使用$parent来 ...

  7. angular中的compile和link函数

    angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...

  8. Deferred在jQuery和Angular中的使用与简单实现

    Deferred在jQuery和Angular中的使用与简单实现 Deferred是在jQuery1.5版本中加入的,并且jQuery使用它完全重写了AJax,以前也只是偶尔使用.但是上次在使用Ang ...

  9. angular源码分析:angular中入境检察官$sce

    一.ng-bing-html指令问题 需求:我需要将一个变量$scope.x = '<a href="http://www.cnblogs.com/web2-developer/&qu ...

随机推荐

  1. hibernate核心开发接口_Configuration

    AnnotationConfiguration继承自Configuration,这里以AnnotationConfiguration为例: new AnnotationConfiguration(). ...

  2. Spark 概念学习系列之Spark Core(十五)

    不多说,直接上干货! 最关键的是转换算子Transformations和缓存算子Actions. 主要是对RDD进行操作. RDD Objects  ->  Scheduler(DAGSched ...

  3. python-Event事件线程同步和互斥

    #!/usr/bin/python #coding=utf-8 #用于线程间通信,通过事件标识控制 import threading from time import sleep,ctime def ...

  4. unity优化测试插件推荐:内存分析,数据监控,弱网模拟

    1.内存分析插件,unity官方出品 官方地址:https://bitbucket.org/Unity-Technologies/memoryprofiler 我整理的:https://downloa ...

  5. PHP之string之ltrim()函数使用

    ltrim (PHP 4, PHP 5, PHP 7) ltrim - Strip whitespace (or other characters) from the beginning of a s ...

  6. memcached 学习笔记 1

    一 简介 1 What is Memcached? Free & open source, high-performance, distributed memory object cachin ...

  7. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

  8. H5微信自定义分享链接(设置标题+简介+图片)

    起源:最近公司在做招募广告的html5页面,然后做出来后,产品提出一个问题,需要分享出去的链接是卡片形式,内容也要自己定义,这下就难到我了,因为是第一次遇到这种需求,果断百度,然而,我就像大家一样,看 ...

  9. 远程服务通讯Service(Remote--AIDL)

    服务端代码:https://github.com/maogefff/AndroidTest/tree/develop-ServiceLocal2 客户端代码:https://github.com/ma ...

  10. python——高级特性

    切片操作符 Python提供了切片(Slice)操作符,切片操作十分有用,可以通过切片轻松取出某一段数列.比如前10个数: #slice切片操作符 取前10个元素 L=list(range(0,100 ...