일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 웹 스터디
- ipTIME
- 웹앱
- Spring Batch
- 서버운영
- reactor core
- spring reactive
- Spring Framework
- 웹 커리큘럼
- reactor
- 공유기 서버
- reactive
- Today
- Total
Hello World
Javascript Date Format Example 본문
Date objects
objects are based on the number of milliseconds since January 1,1970 UTC. They have no syntax, but are stated using constructors, like below:
You can use them like this:
Be careful when invoking the
object, because if you use it without the operator, it will only return the current date and time as a string.The
object has a number of specific qualities to it’s name, for example, if you give it an illogical value, say 3/14/2013, it transforms it into 3/2/2014. If you don’t provide a time and date, the constructor will fill it in with the systems time and date, and if two arguments are supplied, the other ones will be set to 0. JavaScript supports a number of universal formats, and even local equivalents of it, which provides consistency and uniform behavior across platforms.Date.prototype
allows you to add properties to the object. It is used together with the methods, which are divided into getters, setters and conversion getters, but we’ll discus them shortly. The properties inherited from the function are: , , , and .
Methods
The three methods used with the
object are , and .- returns the current time of the system.
- parses a string representation of a date and returns the numeric value corresponding it.
- returns the number of milliseconds since January 1,1970.
Also there are these methods inherited from the function:
, , and .Date instances
The methods that Date instances inherit from Date.prototype can be divided into three big groups:Getters, Setters andConversion getters.
- Getters
We use Getters as here, instead of .
, where you can put , , , and other ones which you can find- Setters
We use Setters in a similar way: here.
. You can find the list on what exactly you can set,- Conversion getters
And finally, we use Conversion getters to get and convert dates at the same time. It is used like this:here.
, and you can find a whole list on what you can convertCalculating elapsed time
By using
objects you can calculate how much time has passed since you last did something, like this:You get the time in a specific moment, then do something for some time, and then get the time after you finish. You get the difference and that is the elapsed time in milliseconds.
JavaScript Libraries
There are lots of JavaScript libraries dedicated to data formatting. Two of the most efficient are Datejs and Moment.js, both open source and released under the MIT license. We’ll see how they work.
Datejs
You can download Datejs here, and then script it in you main file. It gives you the possibility to format you dates according to where you live, giving you more than 150 options. To do that you just swap the file for a culture specific one, like below:
Let’s get our hands on it. Want to know what’s the date today? Write this code:
Or what will be the day ten days from now?
If you wanna know what the date will be this Friday, you write this:
You can even ask if Friday is today, doing this:
And in case it returns false (it can even return true), you can ask what day it is like so:
You can even get the first Monday or last Sunday of the year by writing this:
Included with the Date.js library is a really good replacement for JavaScript Date parse. Take a look at the code:
We have parsed the date of today, tomorrow, July 8th, July 8th of 2007 and July 8th of 2007 at 10:30 PM, just by using the
function.Moment.js
Moment.js is created to parse, validate, manipulate and display dates in JavaScript. It is designed to work both in the browser and Node.js. You can install it through Bower, npm or NuGet.You can format dates like this:
You can get a relative time, like this:
And it even provides you multiple locale support:
Later.js
Later.js is a library that triggers recurring events, and defines complex schedules. It can even use user-friendly expressions such as “every 10 seconds”. You can install it through npm:
Or even using Bower:
You can define a schedule by text, recursion or manually like below:
Still you can define more complex schedules like this one, who fires the weekday closest to the 15th of every month except March. Here’s the code:
You can even configure your timezone, either in UTC format or in local time. You configure it in UTC like this:
And you can turn it to local time like this:
With Library.js you can even calculate future or past occurrences once you set a recurring schedule. Look at the code snippet below:
Also a lot of other functions of this library are available, and you can take a look at it’s documentation on Github or even modify it yourself as it’s open-source.
Download the source code
This was an example of JavaScript date format.
You can download the full source code of this example here : DateFormat
출처: http://www.webcodegeeks.com/javascript/javascript-date-format-example
'Javascript > Core' 카테고리의 다른 글
HTML5 LocalStorage 살펴보기 (0) | 2016.02.18 |
---|---|
[펌] eval() 함수에 대한 재정의 : eval() is not evil?/ (0) | 2016.02.02 |
4 JavaScript Design Patterns You Should Know (0) | 2016.01.10 |
DOM (0) | 2016.01.10 |
Airbnb Javascript Coding Convention (0) | 2016.01.10 |