Javascript library for human-friendly relative date formatting
Since I wrote this answer, a well known library available is moment.js.
There are libraries available, but it is trivial to implement it yourself. Just use a handful of conditions.
Assume date
is an instantiated Date
object for the time you want to make a comparison against.
// Make a fuzzy time
var delta = Math.round((+new Date - date) / 1000);
var minute = 60,
hour = minute * 60,
day = hour * 24,
week = day * 7;
var fuzzy;
if (delta < 30) {
fuzzy = 'just then.';
} else if (delta < minute) {
fuzzy = delta + ' seconds ago.';
} else if (delta < 2 * minute) {
fuzzy = 'a minute ago.'
} else if (delta < hour) {
fuzzy = Math.floor(delta / minute) + ' minutes ago.';
} else if (Math.floor(delta / hour) == 1) {
fuzzy = '1 hour ago.'
} else if (delta < day) {
fuzzy = Math.floor(delta / hour) + ' hours ago.';
} else if (delta < day * 2) {
fuzzy = 'yesterday';
}
You would need to adapt this to handle future dates.
Javascript timestamp to relative time
Well it's pretty easy if you aren't overly concerned with accuracy. What wrong with the trivial method?
function timeDifference(current, previous) {
var msPerMinute = 60 * 1000;
var msPerHour = msPerMinute * 60;
var msPerDay = msPerHour * 24;
var msPerMonth = msPerDay * 30;
var msPerYear = msPerDay * 365;
var elapsed = current - previous;
if (elapsed < msPerMinute) {
return Math.round(elapsed/1000) + ' seconds ago';
}
else if (elapsed < msPerHour) {
return Math.round(elapsed/msPerMinute) + ' minutes ago';
}
else if (elapsed < msPerDay ) {
return Math.round(elapsed/msPerHour ) + ' hours ago';
}
else if (elapsed < msPerMonth) {
return 'approximately ' + Math.round(elapsed/msPerDay) + ' days ago';
}
else if (elapsed < msPerYear) {
return 'approximately ' + Math.round(elapsed/msPerMonth) + ' months ago';
}
else {
return 'approximately ' + Math.round(elapsed/msPerYear ) + ' years ago';
}
}
Working example here.
You might want to tweak it to handle the singular values better (e.g. 1 day
instead of 1 days
) if that bothers you.
PHP library to generate user friendly relative timestamps
Since there didn't seem to be any library, I've made one myself and got it included in PEAR:
Date_HumanDiff, http://pear.php.net/package/Date_HumanDiff
Code is at http://github.com/pear/Date_HumanDiff
Format relative date to human readable format in Android using Android DateUtils API
You can use DateUtils.getRelativeTimeSpanString for that:
long now = System.currentTimeMillis();
DateUtils.getRelativeTimeSpanString(lastUpdated.getTime(), now, DateUtils.DAY_IN_MILLIS);
How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
If you're using Node.js, you're sure to have EcmaScript 5, and so Date has a toISOString
method. You're asking for a slight modification of ISO8601:
new Date().toISOString()
> '2012-11-04T14:51:06.157Z'
So just cut a few things out, and you're set:
new Date().toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, '') // delete the dot and everything after
> '2012-11-04 14:55:45'
Or, in one line: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
ISO8601 is necessarily UTC (also indicated by the trailing Z on the first result), so you get UTC by default (always a good thing).
javascript - change date object to full text representive
You can use the following code in JavaScript. Just pass the date object in function return_formatted_date
, and this function will return text representation as you require. Hope this helps!
<script>
function return_formatted_date(date){
var text = "";
var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var centuryText = ["One", "Two", "Three","Four", "Five", "Six", "Seven", "Eight", "Nine"];
var eleven_twenty = ["Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"];
var hundred_suffix = ["","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"];
text = weekday[date.getDay()] + " the ";
var day = date.getDate();
if(day==1){
text = text + day + "st";
}else if(day==2){
text = text + day + "nd";
}else if(day==3){
text = text + day + "rd";
}else{
text = text + day + "th";
}
text = text + " of " + months[date.getMonth()];
var year = parseInt(date.getFullYear());
var century = parseInt(year/1000);
var hundred = parseInt(year/100)%10;
var tens = parseInt(year)%100;
if(century!=0){
text = text + " " + centuryText[century-1] + " thousand";
}
if(hundred!=0){
text = text + " " + centuryText[hundred-1] + " hundred";
}
if(tens!=0){
text = text + " and";
if(tens<10){
text = text + " " + centuryText[tens-1];
}else if(tens>=11&&tens<=20){
text = text + " " + eleven_twenty[tens-11];
}else{
var tens_up = tens/10;
var tens_down = tens%10;
if(tens_up!=0){
text = text + " " + hundred_suffix[tens_up-1];
}
if(tens_down!=0){
text = text + " " + centuryText[tens_down-1];
}
}
}
return text;
}
</script>
Use Moment.js to convert Unix epoch time to human readable time
moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')
Human friendly date descriptions with NSDate on iOS
This is the solution in Swift 2:
func formattedHumanReadable(date: NSDate) -> String {
let formatter = NSDateFormatter()
formatter.timeStyle = .NoStyle
formatter.dateStyle = .ShortStyle
formatter.doesRelativeDateFormatting = true
let locale = NSLocale.currentLocale()
formatter.locale = locale
return formatter.stringFromDate(date)
}
Related Topics
JavaScript Replace with Reference to Matched Group
Create an Empty Object in JavaScript with {} or New Object()
Getting a Jquery Selector for an Element
Solve Cross Origin Resource Sharing with Flask
How to Store a Global Value (Not Necessarily a Global Variable) in Jquery
How to Send a Message to a Particular Client with Socket.Io
JavaScript Pass Object as Reference
How to Remove Element from Array in Foreach Loop
Differences Between Contenttype and Datatype in Jquery Ajax Function
How to Use Redis Publish/Subscribe with Nodejs to Notify Clients When Data Values Change
Code with Classlist Does Not Work in Ie
How to Use Preload.Js Properly in Electron
Promise Constructor with Reject Call VS Throwing Error
How to Resolve Typeerror: Cannot Convert Undefined or Null to Object
How JavaScript Closures Are Garbage Collected
Es6 Modules Implementation, How to Load a JSON File