Function to display last seen (current date)- Jquery

Asked

Viewed 53 times

0

Hello,

Through the function below, I intend to display the last seen.

However, it is showing the last month, instead of showing the current month.

For example, I am sending the current date but is returning month 2.

jsfiddle

let ret = mDate('2019-03-12 17:41:19').lastSeenFormat()
console.log('last seen ' + ret);

alert('last seen ' + ret);

// code
function mDate(dateString){
	
	let date = dateString ? new Date(dateString) : new Date();

	let dualize = (x) => x < 10 ? "0" + x : x;
	let getTime = () => dualize(date.getHours()) + ":" + dualize(date.getMinutes());
	let getDate = () => dualize(date.getDate()) + "/" + dualize(date.getMonth()) + "/" + dualize(date.getFullYear());

	return {
		subtract: (otherDateString) => {
			return date - new Date(otherDateString);
		},
		lastSeenFormat: () => {
			let dateDiff = Math.round(new Date() - date) / (1000 * 60 * 60 * 24);
			let value = (dateDiff === 0) ? "today" : (dateDiff === 1) ? "yesterday" : getDate();
			return value + " at " + getTime();
		},
		chatListFormat: () => {
			let dateDiff = Math.round((new Date() - date) / (1000 * 60 * 60 * 24));
			if (dateDiff === 0) {
				return getTime();
			} else if (dateDiff === 1) {
				return "Yesterday";
			} else {
				return getDate();
			}
		},
		getDate: () => {
			return getDate();
		},
		getTime: () => {
			return getTime();
		},
		toString:() => {
			return date.toString().substr(4, 20);
		},
	};
};

1 answer

0

The "getMonth()" function of the Date object returns the month number starting with zero, so Jan=0, Feb=1, Mar=2 until Dec=11.

You just need to take this into account that otherwise there is nothing wrong with your script.

  • This would not be the solution getMonth() + 1 so I wouldn’t show the string today.

  • Yes, it’s the simplest way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.