javascript unix timestamp to date

Содержание

  1. JavaScript Datetime: Exercise-17 with Solution
  2. toDateString()
  3. toISOString()

timestamp javascript unix time posix time epoch time

A generic solution to get the Unix timestamp of any JavaScript Date object.

Unix time (AKA epoch time) is used w >time() call and JavaScript’s Date.getTime() :

A Unix timestamp is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

JavaScript’s Date.getTime() returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).

Copy & paste the following code at the beginning of your JavaScript:

JavaScript Datetime: Exercise-17 with Solution

Write a JavaScript function to convert a Unix timestamp to time.

Test Data:
console.log(Unix_timestamp(1412743274));
10:11:14

Sample Solution:-

HTML Code:

JavaScript Code:

Flowchart:

Live Demo:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Unlike other languages, JavaScript doesn’t have equivalent for function strftime. Javascript however provides multiple functions to present date and time in human readable form. They are:

toDateString Converts the date portion of a Date object into a readable string toGMTString Deprecated. Use the toUTCString() method instead toISOString Returns the date as a string, using the ISO standard toJSON Returns the date as a string, formatted as a JSON date toLocaleDateString Returns the date portion of a Date object as a string, using locale conventions toLocaleTimeString Returns the time portion of a Date object as a string, using locale conventions toLocaleString Converts a Date object to a string, using locale conventions toString Converts a Date object to a string toTimeString Converts the time portion of a Date object to a string toUTCString() Converts a Date object to a string, according to universal time

toDateString()

You can learn more about toDateString in documentation.

toISOString()

You can learn more about toISOString in documentation.

Источник: computermaker.info

Техника и Гаджеты
Добавить комментарий