View JSON item in current time - javascript

I would like to print only 2 results of json but taking those included in current time.
Example: at 11.00 am today will be printed only the second and third item
JAVASCRIPT
$(data.users).each(function() {
var output = "<ul><li>" + this.firstName + " " + this.lastName + "--" + this.date + this.hour+" </li>
</ul>";
$('#placeholder').append(output);
});
Example: JSFIDDLE
Thanks!

Try this -
$(data.users).each(function() {
var date = new Date();
var month = (date.getMonth() + 1);
month = month < 10 ? '0' + month : month;
var dayStr = date.getFullYear() + '' + month + '' + date.getDate();
var hour = date.getHours() < 10 ? '0' + date.getHours(): date.getHours();
var mins = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var time = hour + '' + mins;
if(this.date == dayStr && parseInt(this.hour, 10) >= parseInt(time, 10))
var output = "<ul><li>" + this.firstName + " " + this.lastName + "--" + this.date + this.hour+"</li></ul>";
$('#placeholder').append(output);
});

You can use filter:
data.users.filter(function(element) { return element.hour === "1100" })
You can modify the actual test based on how you'll get the current time, whether you want to round to the hour, etc.

Related

How to write a method which gives me future date time in format YYYY/MM/DD 12:30:50

I have written below method for this but it will fail when the current date will be 31.
I need to check if date is 31 it should return me 1st date of next month. Any help would be appreciated
getFutureDateTime: function () {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate() + 1;// to get current date remove "+1"
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if (month.toString().length == 1) {
month = '0' + month;
}
if (day.toString().length == 1) {
day = '0' + day;
}
if (hour.toString().length == 1) {
hour = '0' + hour;
}
if (minute.toString().length == 1) {
minute = '0' + minute;
}
if (second.toString().length == 1) {
second = '0' + second;
}
var dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
return dateTime;
},
It looks like you're trying to get the next day as a string. Your best bet is to let the Date object do the rollover between months and years for you, like this:
getFutureDateTime: function () {
var dt = new Date();
dt.setDate(dt.getDate() + 1); // Will handle rollover for you
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var day = dt.getDate();
var hour = dt.getHours();
var minute = dt.getMinutes();
var second = dt.getSeconds();
if (month.toString().length == 1) {
month = '0' + month;
}
if (day.toString().length == 1) {
day = '0' + day;
}
if (hour.toString().length == 1) {
hour = '0' + hour;
}
if (minute.toString().length == 1) {
minute = '0' + minute;
}
if (second.toString().length == 1) {
second = '0' + second;
}
var dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
return dateTime;
},
Note that if you're doing this in any vaguely modern environment, you can use padStart on the string (and padStart is easily polyfilled):
getFutureDateTime: function () {
var dt = new Date();
dt.setDate(dt.getDate() + 1); // Will handle rollover for you
var dateTime =
year.toString().padStart(2, "0") +
"/" +
month.toString().padStart(2, "0") +
"/" +
day.toString().padStart(2, "0") +
" " +
hour.toString().padStart(2, "0") +
":" +
minute.toString().padStart(2, "0") +
":" +
second.toString().padStart(2, "0");
return dateTime;
},
You could give yourself a utility function for the padding, to avoid repeating yourself:
function padZero2(val) {
return String(val).padStart(2, "0");
}
// ...
getFutureDateTime: function () {
var dt = new Date();
dt.setDate(dt.getDate() + 1); // Will handle rollover for you
var dateTime =
padZero2(year) +
"/" +
padZero2(month) +
"/" +
padZero2(day) +
" " +
padZero2(hour) +
":" +
padZero2(minute) +
":" +
padZero2(second);
return dateTime;
},
Similarly, if you use an ES2015 template literal, it may be a bit clearer:
getFutureDateTime: function () {
const dt = new Date();
dt.setDate(dt.getDate() + 1); // Will handle rollover for you
const dateTime = `${padZero2(year)}/${padZero2(month)}/${padZero2(day)} ${padZero2(hour)}:${padZero2(minute)}:${padZero2(second)}`;
return dateTime;
},
You don't need to have that complex function, look at this:
function getFutureDateTime() {
const regex = /(^[0-9-]+)(t)([^Z.]+)/i;
const date = new Date();
const isoFutureDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1).toISOString();
const matches = iso.match(regex);
return matches[1] + ' ' + matches[3];
}
m= require("moment")
console.log(m().add("months",2).format("YYYY/MM/DD HH:MM:SS"))
use momentjs why to reinvent wheel when you already have some nodejs library for that you can change months to days , years etc to add days,houts,years etc instead of month
https://momentjs.com/guides/#/warnings/add-inverted-param/
You sould probably add an if statement before adding the '0' to test if day==32 => day = 1 and month = month+1
getFutureDateTime: function () {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate() + 1;// to get current date remove "+1"
if (day==32){
day = 1;
month = month + 1;
}
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if (month.toString().length == 1) {
month = '0' + month;
}
if (day.toString().length == 1) {
day = '0' + day;
}
if (hour.toString().length == 1) {
hour = '0' + hour;
}
if (minute.toString().length == 1) {
minute = '0' + minute;
}
if (second.toString().length == 1) {
second = '0' + second;
}
var dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
return dateTime;
},

js dates pass in a function

I am facing an issue in javascript dates, i want to added this lines in my GetFormattedDate function.
I try , i can't implement this logic in my function
var currentdate = new Date();
var myTime1 = currentdate.getHours() +':'+ (currentdate.getMinutes() <= 29 ? '00' : '30') ; //output 18:43
My code:
function GetFormattedDate(date) {
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + (date.getDate())).slice(-2);
var year = date.getFullYear();
var hour = ("0" + (date.getHours())).slice(-2);
var min = ("0" + (date.getMinutes())).slice(-2);
var seg = ("0" + (date.getSeconds())).slice(-2);
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg + " " ;
}
Expected output
`2020-05-12 01:00:00` //if minutes are 0 to 29 then show current hours reset the minutes again start with 0 like 18:00:00 and seconds become 0
`2020-05-12 01:30:00 ` //if minutes are 29 to 59 then show current hours reset the minutes again start with 30 like 18:30:00 and seconds become 0
Do it when you set the min and seg variables
Replace the two lines
var min = ("0" + (date.getMinutes())).slice(-2);
var seg = ("0" + (date.getSeconds())).slice(-2);
with
var min = date.getMinutes() <= 29 ? '00' : '30';
var seg = '00';
function GetFormattedDate(date) {
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + (date.getDate())).slice(-2);
var year = date.getFullYear();
var hour = ("0" + (date.getHours())).slice(-2);
var min = date.getMinutes() <= 29 ? '00' : '30';
var seg = '00';
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg + " ";
}
console.log(GetFormattedDate(new Date));
You're passing a string to your function. Based on the link you provided in a comment, you need to parse a string representation of a date into an actual date object: var d = Date.parse("March 21, 2012"); Read more here: https://www.w3schools.com/jsref/jsref_parse.asp
Once you have a new Date object, set its seconds:
var d = new Date();
d.setSeconds(d.getSeconds() <= 29 ? 0 : 30);
Now you can pass d to your function:
GetFormattedDate(d);

Javascript Slicing date/time with leading zero

I'm trying to add a leading 0 before a certain part of a date. For example, if it's 9:00am, I want to display 09:00 and not 9:0. I want to be able to add a leading zero, so I can insert it into MySQL coding.
The result I'm getting is
2018-05-029 019:07:016
Here is my Javascript code:
var login_date="";
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth()+1; /*months are from 0 - 11 */
month = '0' + month.toString().slice(-2);
var day = d.getDate();
day = '0' + day.toString().slice(-2);
var hour = d.getHours();
hour = '0' + hour.toString().slice(-2);
var minute = d.getMinutes();
minute = '0' + minute.toString().slice(-2);
var second = d.getSeconds();
second = '0' + second.toString().slice(-2);
login_date = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
console.log(login_date);
You can check for the variable length of characters, if is less than two, then add a 0.
Something like this:
var d = new Date();
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();
if (month.toString().length < 2) month = '0' + month;
if (hour.toString().length < 2) hour = '0' + hour;
if (minute.toString().length < 2) minute = '0' + minute;
if (second.toString().length < 2) second = '0' + second;
console.log(year + '-' + month + '-' + day + " " + hour + ":" + minute + ":" + second)
You could just check if the value is smaller then 10 to add an "0" at the beginning.
example
var seconds = seconds < 10 ? '0'+seconds : seconds;
Your final string could be defined like:
var login_date = year + "-"
+ (month < 10 ? "0" + month : month) + "-"
+ (day < 10 ? "0" + day : day) + " "
+ (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute) + ":"
+ (second < 10 ? "0" + second : second) ;
You can create a function addZero() that handles the concatenation of a 0 if necessary. Here is the code:
let addZero = (el) => ((el.toString().length == 1) ? '0' : '') + el.toString();
var login_date = "";
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1; /*months are from 0 - 11 */
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();
login_date = year + "-" + addZero(month) + "-" + addZero(day) + " " + addZero(hour) + ":" + addZero(minute) + ":" + addZero(second);
document.write(login_date);

SetTimeout is returing wrong value

function updateTime() {
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
var d = day + "-" + month + "-" + year;
var hours = currentDate.getHours() < 10 ? "0" + currentDate.getHours() : currentDate.getHours();
var minutes = currentDate.getMinutes() < 10 ? "0" + currentDate.getMinutes() : currentDate.getMinutes();
var seconds = currentDate.getSeconds() < 10 ? "0" + currentDate.getSeconds() : currentDate.getSeconds();
var t = hours + " " + minutes + " " + seconds;
var x = d + " " + t;
return x;
}
console.log(updateTime());
var timerId = setTimeout(updateTime(), 1000);
alert(timerId);
Am trying to do a javascript timer. Here UpdateTime function is working properly. Its returing the exact result. But time is constantly being displayed. It should change according to the system time. Hence am using the setTimeout function. But its not returing the proper value. Can anyone help me out here please?
You should remove the alert and () from setTimeout's callback function,
Your code should be
function updateTime() {
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
var d = day + "-" + month + "-" + year;
var hours = currentDate.getHours() < 10 ? "0" + currentDate.getHours() : currentDate.getHours();
var minutes = currentDate.getMinutes() < 10 ? "0" + currentDate.getMinutes() : currentDate.getMinutes();
var seconds = currentDate.getSeconds() < 10 ? "0" + currentDate.getSeconds() : currentDate.getSeconds();
var t = hours + " " + minutes + " " + seconds;
var x = d + " " + t;
return x;
}
setTimeout(updateTime, 1000);
timer in javascript with current date
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var myVar = setInterval(myTimer ,1000);
function myTimer() {
var d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>
</body>
</html>
what would you do ?
I just edit Your code hope it will help you
function updateTime() {
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
var d = day + "-" + month + "-" + year;
var hours = currentDate.getHours() < 10 ? "0" + currentDate.getHours() : currentDate.getHours();
var minutes = currentDate.getMinutes() < 10 ? "0" + currentDate.getMinutes() : currentDate.getMinutes();
var seconds = currentDate.getSeconds() < 10 ? "0" + currentDate.getSeconds() : currentDate.getSeconds();
var t = hours + " " + minutes + " " + seconds;
var x = d + " " + t;
alert(x);/*display time */
}
setTimeout(updateTime, 1000);/* just remove () braces from your code */

Passing Date argument to function

How to pass argument of Date to another function? My code:
var myDate = new Date(data.GetOPCResult.DateTime.match(/\d+/)[0] * 1);
var datlabel = document.getElementById("ct");
datlabel.innerHTML = GetTime(myDate);
And GetTime function code:
function GetTime(DateTime) {
var month = (DateTime.getMonth() < 10) ? "0" + (DateTime.getMonth() + 1) : (DateTime.getMonth() + 1);
var day = (DateTime.getDate() < 10) ? "0" + DateTime.getMonth() : DateTime.getMonth();
var hour = (DateTime.getHours() < 10) ? "0" + DateTime.getHours() : DateTime.getHours();
var minute = (DateTime.getMinutes() < 10) ? "0" + DateTime.getMinutes() : DateTime.getMinutes();
var second = (DateTime.getSeconds() < 10) ? "0" + DateTime.getSeconds() : DateTime.getSeconds();
return DateTime.getDate() + "." + month + "." + DateTime.getFullYear() + " " + hour + ":" + minute + ":" + second;
}
This works for me
function GetTime(d) {
var month = (d.getMonth() < 10) ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1);
var day = (d.getDate() < 10) ? "0" + d.getMonth() : d.getMonth();
var hour = (d.getHours() < 10) ? "0" + d.getHours() : d.getHours();
var minute = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes();
var second = (d.getSeconds() < 10) ? "0" + d.getSeconds() : d.getSeconds();
return d.getDate() + "." + month + "." + d.getFullYear() + " " + hour + ":" + minute + ":" + second;
}
alert(GetTime(new Date()));
Are you sure you are passing a valid Date object? Try passing new Date() instead of myDate to your GetTime. If that works, your myDate variable is not a valid Date object.
Your code is fine. A little re-factoring will help though.
function GetTime(date) {
var day = zeroPad(date.getDate(), 2);
var month = zeroPad(date.getMonth() + 1, 2);
var year = zeroPad(date.getFullYear(), 4);
var hour = zeroPad(date.getHours(), 2);
var minute = zeroPad(date.getMinutes(), 2);
var second = zeroPad(date.getSeconds(), 2);
return day + "." + month + "." +
year + " " + hour + ":" +
minute + ":" + second;
}
function zeroPad(num, count) {
var z = num + '';
while (z.length < count) {
z = "0" + z;
}
return z;
}
Also please check what is data.GetOPCResult.DateTime. I would say this will do.
var myDate = new Date( (data.GetOPCResult.DateTime || "")
.replace(/-/g,"/")
.replace(/[TZ]/g," ") );

Categories