Coder Social home page Coder Social logo

time.js's Introduction

time.js

格式化时间戳模块

安装

1. bower

bower install binnng/time.js --save

2. component

component install binnng/time.js --save

3. npm

npm install time.js --save

使用

1. 直接引用

<html>
<script type="text/javascript" src="time.js"></script>
<script type="text/javascript">
	var time = + new Date;

	Time(time, "%y年%M月%d日%h时%m分%s秒"); // xxxx年xx月xx日xx时xx分xx秒
	Time(time, "%y-%M-%d %h:%m:%s"); //xxxx-xx-xx xx:xx:xx

	Time.ago(time); // 刚刚

</script>
</html>

demo

2. Angular

var myApp = angular.module("myApp", [
	"binnng/time"
]); 
factory
myApp.controller("timeCtrl", function($scope, $time) {  
	var time = + new Date;
	
	$time(time, "%y年%M月%d日%h时%m分%s秒"); // xxxx年xx月xx日xx时xx分xx秒
	$time(time, "%y-%M-%d %h:%m:%s"); //xxxx-xx-xx xx:xx:xx
});
filter

time.js为angular提供了filter,你可以这样使用

<p>{{timeText | date}}</p> <!-- // xxxx年xx月xx日 -->
<p>{{timeText | datetime}}</p> <!-- // xxxx年xx月xx日xx时xx分xx秒 -->
<p>{{timeText | ago}}</p> <!-- x个月前 -->

demo

API

Time()

根据你自定义的格式返回时间字符串

Time(time, "%y年%M月%d日%h时%m分%s秒"); // xxxx年xx月xx日xx时xx分xx秒

Time.ago()

传入的时间戳距离现在有多久

  • 刚刚
  • 1天前
  • 3个月前
  • 10年前
Time.ago(1413772288388); // 一天前

源码

源码

time.js's People

Contributors

binnng avatar

Watchers

James Cloos avatar ²¹带你去旅行♬ ♫♬ avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.