Getting value of specific time in next week - javascript

I am trying to get the value of next week ( + 7 days) at 09:00. I can get the Date using
new Date().setDate(new Date().getDate() + 7)
For example, it returns: 1619343639426
which translates to
new Date(1619343639426)
Sun Apr 25 2021 15:10:39 GMT+0530 (India Standard Time)
I want to get the value for Sun Apr 25 2021 09:00:00 GMT+0530 (India Standard Time)
how to do that ?

Try
new Date(new Date().setDate(new Date().getDate() + 7)).setHours(9, 0, 0, 0)

Related

Why does new Date(2018,12, 25) give the wrong month and year? [duplicate]

This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 4 years ago.
I'm completely lost as to why the following code gives me a year of 2021 while executing this on the day of 01/03/2019... What... What am I missing... I so need a coding buddy sitting next to me I think.. >.<
function daysTillChristmas()
{
var today = new Date();
var christmasDay1 = new Date(today.getFullYear() +1, 12, 25);
var test = today.getFullYear() + 1;
var christmasDay2 = new Date(2020, 12, 25);
console.log(today);
console.log("Today: " + today);
console.log("Today Full Year: " + today.getFullYear());
console.log("ChristmasDay1: " + christmasDay1);
console.log(christmasDay2);
console.log("ChristmasDay2: " + christmasDay2);
console.log("Test: " + test);
}
My output is...
"2019-01-03T22:54:33.294Z"
Today: Thu Jan 03 2019 14:54:33 GMT-0800 (Pacific Standard Time)
Today Full Year: 2019
ChristmasDay1: Mon Jan 25 2021 00:00:00 GMT-0800 (Pacific Standard Time)
"2021-01-25T08:00:00.000Z"
ChristmasDay2: Mon Jan 25 2021 00:00:00 GMT-0800 (Pacific Standard Time)
Test: 2020
BTW... I'm learning Javascript at the moment and I'm using the following URL to test out code and get some exercises to use.
Browser: Chrome 70.0.3538.77 32-bit
https://www.w3resource.com/javascript-exercises/javascript-basic-exercises.php#EDITOR
See MDN:
Note: The argument monthIndex is 0-based. This means that January = 0 and December = 11.
So month 12 is the 13th month of the year, which rolls over and becomes January of the next year.

Why this JavaScript date() is weird?

When I run the following JavaScript code it returns
new Date(2017, 5, 31)
// Sat Jul 01 2017 00:00:00 GMT+0530 (IST)
Here I understand months are zero based in Date() so it overflows to July. But when I run following
new Date(2017, 12, 31)
// Wed Jan 31 2018 00:00:00 GMT+0530 (IST)
Here why the date is Jan 31 instead of throwing an exception?
new Date(2017, 13, 31)
// Sat Mar 03 2018 00:00:00 GMT+0530 (IST). Why Mar 03 instead of Mar 31?
Thanks
new Date(2017, 5, 31)
// Sat Jul 01 2017 00:00:00 GMT+0530 (IST)
June has only 30 days, so the balance 1 day (31 - 30 = 1) overflow to become July 01.
new Date(2017, 12, 31)
// Wed Jan 31 2018 00:00:00 GMT+0530 (IST)
Similarly, year 2017 has only 12 months, so the balance 1 month overflow to become 2018 Jan. Coincidently, January has 31 days too, so it becomes 2018 Jan 31 (31 - 31 = 0).
new Date(2017, 13, 31)
// Sat Mar 03 2018 00:00:00 GMT+0530 (IST). Why Mar 03 instead of Mar 31?
By that logic, year 2017 has only 12 months, so the balance 2 months overflow to become 2018 February.
Unfortunately, Febraury of 2018 has only 28 days, so the balance 3 days (31 - 28 = 3) overflow to become March 03.

Date list using javascript simple

Hello guys could give me a little help ?
Code here https://jsfiddle.net/pedrowperez/hgb5ufqw/2/`
this is my work.
HTML
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
<p id="demo5"></p>
<p id="demo6"></p>
<p id="demo7"></p>
Javascript
var someDate = new Date();
var Ano_Confirmado = someDate.getFullYear();
var Mes_Confirmado = someDate.getMonth();
var Dia_Confirmado = someDate.getDate();
var Week = 7;
var Day = 1;
var someDateD = new Date(Ano_Confirmado, Mes_Confirmado, (Dia_Confirmado) - Week);
document.getElementById('demo1').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo2').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo3').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo4').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo5').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo6').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo7').innerHTML = (someDateD);
Result
Tue Jan 26 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Wed Jan 27 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Thu Jan 28 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Fri Jan 29 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Sat Jan 30 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Sun Jan 31 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Mon Feb 01 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Tue Feb 02 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Wed Feb 03 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
I need to change the date format : Wed Mar 23 2016 15:19:36 GMT-0300 (E. South America Standard Time)
for this format: DD/MM
But I can not find anything Referring dates will list in javascript;
There are two options that come to mind:
Use the Date methods, though this is relatively limiting
Use a library like Moment.js.
Using Date methods
someDate.getDay() + '/' + (someDate.getMonth() + 1);
The (someDate.getMonth() + 1) is required because getMonth() returns the zero-indexed month. Meaning January is 0, February is 1 and so on.
Using Moment.js
Look into a library like . Using Moment you can format dates in endless ways:
moment(someDateD).format('DD/MM');
Here's your updated JSFiddle.
You can create your own function that handles it if you don't need any other complex manipulations with dates.
function formateDate(date) {
function addZero_(num) {
return num < 10 ? ('0' + num) : num);
}
var day = date.getDate();
var month = date.getMonth() + 1;
return addZero_(day) + '/' + addZero_(month);
}
document.getElementById('demo3').innerHTML = formateDate(someDateD);
Also use for to set date values in dom.

how to add days to datetime in javascript?

I have date and time format datetimepicker. I would like to add number of days to the datetime
inputdattime will be picked from datetimepicker
input datetime = Friday, October 23rd 2015, 12:00:00 am
output should be if i add 2 days sunday, oct 25 2015 12:00:00 am
how to achieve this
var deal_from_date = new Date();
var numberOfDaysToAdd = 7;
var date1 = deal_from_date.setDate(deal_from_date.getDate() + numberOfDaysToAdd);
actual Output should be
"dddd, MMMM Do YYYY, h: mm:ss a"
Sun Oct 25 2015 12:00:00
current output is:
Sun Oct 25 2015 12:54:06
setDate() method can be used to add days to a date
var today=new Date('23/10/2015');
var in_a_day=new Date(today).setDate(today.getDate()+2);
Try this code :-
var d = new Date('oct 25 2015 12:00:00 pm');
d.setDate(d.getDate() + 2)
alert(d);
output is :- Tue Oct 27 2015 12:00:00 GMT+0530 (IST)

Convert UTCString to yyyy-mm-dd

I am trying UTCString to above format. I can able to convert, problem is after conversion it shows a day before.
var newDate = this.getCellDate(target);
console.log(newDate); --> Dec 05 2014 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
cstDate = newDate.toISOString();
console.log(cstDate); -- > 2014-12-04 --- > **Expected --> 2014-12-05**
Use Date.UTC() method
var now = new Date(), // my date Thu Dec 04 2014 13:02:15 GMT+0300 (RTZ 2 (зима))
year = now.getFullYear(),
month = now.getMonth(),
day = now.getDay(),
hours = now.getHours(),
minutes = now.getMinutes(),
utcDate;
utcDate = new Date(Date.UTC(year, month, day, hours, minutes)); // Thu Dec 04 2014 16:02:00 GMT+0300 (RTZ 2 (зима))
Ext.Msg.alert('UTC Date', Ext.Date.format(utcDate, 'Y-m-d'));
Look at this "Thu Dec 04 2014 16:02:00" - i got utc time(+3 hours)
Fiddle example
Yeah i got the solution. I should not toISOString. instead i need to use toLocaleDateString
custdate = newDate.toLocaleDateString();
dueDate= custdate.split("/").reverse().join("-");

Categories