I am trying to make a simple JS calendar which will indicate the current date and then 90 dates after this date. I want it to be displayed as, example: Monday, May 6, 2019 -and- Sunday August 4, 2019. Can you tell me why my second date is displaying differently? Thank you! (I've attached the html file with the code).
I am able to set both dates to display like: 05/06/2019 and 08/04/2019, but when I attempt to change and use the arrays to display to the above syntax, the second date does not display correctly.
I have created a JS Fiddle that shows the code and the result, here:
https://jsfiddle.net/Catja_M/68q0s7fy/2/
var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;}
today=days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear()));
document.write(today);
var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new
Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;}
var today=new Date()
new Date(days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())));
today.setDate(today.getDate()+90)
document.write(today);
Looking to achieve both dates to display the same: Monday, May 6, 2019.
change this:
var today=new Date()
new Date(days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())));
today.setDate(today.getDate()+90)
document.write(today);
to:
now.setDate(now.getDate() + 90) //this returns a total millis number not a "date", it needs to be converted to day/month/year
var date = ((now.getDate()<10) ? "0" : "") + now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
}
var today = days[now.getDay()] + ", " + months[now.getMonth()] + " " + date + ", " + fourdigits(now.getYear());
document.write(today);
Related
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
I have looked at a number of questions concerning how this is done but none of them work with my code. I am trying to findout if the "nowdate" is less than "startdate". The code below is what I have so far:
INPUT :
Start date: 01-03-2019 00:00
End date: 31-03-2019 23:59
My code;
var nowdate;
var d = new Date();
nowdate = ('0' + d.getDate()).slice(-2) + "-" + ('0' + (d.getMonth() + 1)).slice(-2) + "-" + d.getFullYear() + " " + ('0' + d.getHours()).slice(-2) + ":" + ('0' + d.getMinutes()).slice(-2);
This produces : Now date: 17-02-2019 16:43
if(nowdate < startdate){
console.log("Start date is greater than now date");
} else {
console.log("Start date is NOT greater than now date")
}
This does not produce the result I am expecting. Can anyone see why.
Many thanks in advance for your help and time.
You should use Date.prototype.getTime() when you want to compare two dates
let startDate = (new Date('01-03-2019 00:00')).getTime()
let nowDate = (new Date()).getTime();
if(nowDate < startDate){
console.log("Start date is greater than now date");
}
else{
console.log("Start date is NOT greater than now date")
}
I'm using ASP.NET and I have the following string:
2018-12-04T13:53:42.6785734+07:00
I want to convert the string to Date object and format the output string.
My goal: 04/12/2018 13:53:42
I've tried this way but it logged with the wrong result:
var dt = new Date('2018-12-04T13:53:42.6785734+07:00');
var day = dt.getDay(),
month = dt.getMonth(),
year = dt.getFullYear(),
hours = dt.getHours(),
minutes = dt.getMinutes(),
seconds = dt.getSeconds();
// 2/11/2018 13:53:42
console.log(day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds);
var dt = new Date('2018-12-04T13:53:42.6785734+07:00');
var day = dt.getDate(),
month = dt.getMonth(),
year = dt.getFullYear(),
hours = dt.getHours(),
minutes = dt.getMinutes(),
seconds = dt.getSeconds();
// 2/11/2018 13:53:42
console.log(day + '/' + (month + 1) + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds);
I changed your code like this, try:
var dt = new Date('2018-12-04T13:53:42.6785734+07:00');
var day = dt.getDate(),
month = dt.getMonth(),
year = dt.getFullYear(),
hours = dt.getHours(),
minutes = dt.getMinutes(),
seconds = dt.getSeconds();
console.log(day + '/' + (month + 1) + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds);
Would it be acceptable to just manipulate the original string directly without converting to a date object like so? I can really tell what the test cases would be but assuming that the hour number is the same length (04,12) I think the following code should work.
let dt = '2018-12-04T13:53:42.6785734+07:00';
let dArr = dt.substr(0,10).split('-');
let year = dArr[0];
let month = dArr[1];
let day = dArr[2];
let time = dt.substr(11,8);
let str = month+'/'+day+'/'+year+' '+time;
console.log(str)
The following are arrays starting at 0
getDay() Get the weekday as a number (0-6)
getDate() Get the day as a number (1-31)
getMonth() Get the month as a number (0-11)
var dt = new Date("2018-12-04T13:53:42.6785734+07:00");
var day = returnDay(),
month = returnMonth(),
year = dt.getFullYear(),
hours = dt.getHours(),
minutes = dt.getMinutes(),
seconds = dt.getSeconds(),
function returnDay(){
var d = (dt.getDate() < 10) ? "0" + (dt.getDate()): dt.getDate();
return d;
}
function returnMonth(){
var m = (dt.getMonth() + 1 < 10) ? "0" + (dt.getMonth()+ 1):dt.getMonth()+ 1;
return m;
}
//04/12/2018 8:53:42
console.log(day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds);
see for more info:
https://www.w3schools.com/js/js_date_methods.asp
Slice it, I have used jQuery in the example because I have used in in another project and this code was just a part of a component, but it should not matter much
var getTimeOnly = new Date().getTime();
var fullDate = new Date($.now()).toString();
var datePartialTime = fullDate.slice(16, 25);
var datePartialDate = fullDate.slice(0, 16);
$('a').html( "on " + datePartialDate + 'at ' + datePartialTime);
Link to pen
https://codepen.io/damPop/pen/zMzBGN
I am trying to split a date using the following piece of JavaScript
var dSplit = getDate.split("/");
var newDate = dSplit[2] + "-" + dSplit[0] + "-" + dSplit[1];
I get the following output
2014 12:00:00 AM-11-25
The output i require is
2014-11-25 12:00:00 AM
Please Help.
One possible approach:
var getDate = '11/25/2014 12:00:00 AM';
var newDate = getDate.replace(/^\S+/, function(date) {
var d = date.split('/');
return d[2] + '-' + d[0] + '-' + d[1];
});
// 2014-11-25 12:00:00 AM
This approach allows to process both datetime strings (similar to '11/25/2014 12:00:00 AM', like in your answer) and date strings (like '11/25/2014'). The key here is processing only first sequence of non-whitespace characters in the string.
You may format the date to string as you want, using the next function:
function formatDate(date) {
var ans = date.getFullYear();
ans += "-" + (date.getMonth()+1);
ans += "-" + date.getDay();
ans += " " + date.getHours();
ans += ":" + date.getMinutes();
document.write (ans);
}
This way, even if the user's browser converts date to string on different order (longer format etc.) you have full control on the output string.
This may be helpfull,pass$val alone in function
var dateString=$val.split(" ");
var dateformat=dateString[0].split("-");
var dateVal= dateformat[0] + "/" + dateformat[1] + "/" + dateformat[2];
$.date = function(dateObject) {
var d = new Date(dateObject);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var date = year + "-" + month + "-" + day;
return date;
};
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.