JavaScript getdate
Get date/time information
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 | function getdate (timestamp) { // Get date/time information // // version: 910.813 // discuss at: http://phpjs.org/functions/getdate // + original by: Paulo Ricardo F. Santos // + input by: Alex // + bugfixed by: Brett Zamir (http://brett-zamir.me) // * example 1: getdate(1055901520); // * returns 1: {'seconds': 40, 'minutes': 58, 'hours': 21, 'mday': 17, 'wday': 2, 'mon': 6, 'year': 2003, 'yday': 167, 'weekday': 'Tuesday', 'month': 'June', '0': 1055901520} var _w = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var _m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var d=( (typeof(timestamp) == 'undefined') ? new Date() : // Not provided (typeof(timestamp) == 'object') ? new Date(timestamp) : // Javascript Date() new Date(timestamp*1000) // UNIX timestamp (auto-convert to int) ); var w = d.getDay(); var m = d.getMonth(); var y = d.getFullYear(); var r = {}; r.seconds = d.getSeconds(); r.minutes = d.getMinutes(); r.hours = d.getHours(); r.mday = d.getDate(); r.wday = w; r.mon = m + 1; r.year = y; r.yday = Math.floor((d - (new Date(y, 0, 1))) / 86400000); r.weekday = _w[w]; r.month = _m[m]; r['0'] = parseInt(d.getTime() / 1000, 10); return r;} |
Examples
Running
1 | getdate(1055901520); |
Should return
1 | {'seconds': 40, 'minutes': 58, 'hours': 21, 'mday': 17, 'wday': 2, 'mon': 6, 'year': 2003, 'yday': 167, 'weekday': 'Tuesday', 'month': 'June', '0': 1055901520} |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have getdate goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>