2

I've been searching how to convert UNIXTIME to YY-MM-DD format using javascript, but so far I've only found methods to do it the other way around (YY-MM-DD to UNIXTIME).

Is there any method already incorporated in jquery or something to do this work?

Thanks!!

Joaquin
  • 43
  • 1
  • 1
  • 5
  • Make a new Date object with your Unix timestamp, then you can extract all the info you need with the date methods and make a string of your choice. Check here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – elclanrs Jun 27 '13 at 22:56
  • Closely related searches: [Convert UNIX Time to mm/dd/yy hh:mm (24 hour) in JavaScript](http://stackoverflow.com/questions/3187790/convert-unix-time-to-mm-dd-yy-hhmm-24-hour-in-javascript) and [How to show a Unix-time in a local time format](http://stackoverflow.com/questions/14617305/how-to-show-a-unix-time-in-a-local-time-format) – Xotic750 Jun 27 '13 at 23:47

2 Answers2

1
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;

var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = yyyy.toString().substr(2,2)+'-'+mm+'-'+dd;
document.getElementById("output").innerHTML = today;

This will do the job ..

Of course your new Date() will be your unix time..

http://jsfiddle.net/UpMU5/

Pogrindis
  • 7,755
  • 5
  • 31
  • 44
  • Your output produces "M-D-YY` and not `YY-MM-DD` (in fact your jsfiddle produces `M/D/YY`) – Xotic750 Jun 27 '13 at 23:24
  • Updated it now .. Very simple to change... Very harsh for a downvote. – Pogrindis Jun 27 '13 at 23:27
  • 1
    The down vote is to bring it to your attention and will be removed when fixed. Your update now produces `YY-M-D` and not `YY-MM-DD` – Xotic750 Jun 27 '13 at 23:29
  • you were right! Damn these late nights! ;) Updated ! - By the way your answer is probably better, this is just a way of doing without any extra libs! – Pogrindis Jun 27 '13 at 23:46
1

You could use a library like moments.js or in POJS

Moments.js

A 5.5kb javascript date library for parsing, validating, manipulating, and formatting dates.

unixtime

Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,[note 1] not counting leap seconds.[note 2] It is used widely in Unix-like and many other operating systems and file formats. Due to its handling of leap seconds, it is neither a linear representation of time nor a true representation of UTC.[note 3] Unix time may be checked on some Unix systems by typing date +%s on the command line.

Javascript Date object

Summary

Creates JavaScript Date instances which let you work with dates and times.

Javascript

function padZero(number) {
    if (number < 10) {
        number = "0" + number;
    }

    return number;
}

function unixtime2YYMMDD(unixtime) {
    var milliseconds = unixtime * 1000,
        dateObject = new Date(milliseconds),
        temp = [];

    temp.push(dateObject.getUTCFullYear().toString().slice(2));
    temp.push(padZero(dateObject.getUTCMonth() + 1));
    temp.push(padZero(dateObject.getUTCDate()));

    return temp.join("-");
}

console.log(unixtime2YYMMDD(1372069271));

Output

13-06-24 

On jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79