Determine start and end time of current day (UTC) (JAVASCRIPT) [duplicate] - javascript

This question already has answers here:
How to get start and end of day in Javascript?
(13 answers)
Closed 7 years ago.
I am trying to determine start and end time of the current day in UTC.
Currently this is my solution. `
var starttime = date.getTime() -(date.getHours() * 3600000 ); //subtracting a hours to go back to start time of the day.
var endtime = low + 86400000; //adding 24 hours to the start time
`
Any help is appreciated ! Thank you

Thank you everyone. This is what worked for me !! :)
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var startHour =Date.UTC(year,month,day,0,0,0,0);
var endHour = startHour + 86400000;

Related

I am Working on Javascript Dates, i am stuck with adding 7 days to a date [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 2 years ago.
var date = new Date();
var first_date = new Date(date); //Make a copy of the date we want the first and last days from
first_date.setUTCDate(1); //Set the day as the first of the month
var firstDay = first_date.toJSON().substring(0, 10);
console.log(firstDay)
I am Working on Javascript Dates, i am stuck with adding 7 days to this date
Thanks in advance
var date = new Date();
var first_date = new Date(date); //Make a copy of the date we want the first and last days from
first_date.setUTCDate(1); //Set the day as the first of the month
var firstDay = first_date.toJSON().substring(0, 10);
var resultDate = new Date();
resultDate.setDate(first_date.getDate() + 7);
var resultDay = resultDate.toJSON().substring(0, 10);
console.log("First day: " + firstDay)
console.log("7 days from specific day: " + resultDay)

How to get the date from x-th day of the year? [duplicate]

This question already has answers here:
how can I convert day of year to date in javascript?
(9 answers)
Closed 4 years ago.
From this question I get a very useful function, which can calculate the day of the year from a date.
My question is that how can I reverse it?
So when I pass a number to the function's parameter, which is the day of the year, the function returns a date?
Here's the code:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Here is a function if you pass the year and the nth day of that year this will return the date of that nth day.
function dateFromDay(year, day){
var date = new Date(year, 0);
return new Date(date.setDate(day));
}
console.log(dateFromDay(2010, 301));
console.log(dateFromDay(2010, 365));
I think that this question should answer your problem.
Simply instantiate a new Date at the beginning of the year - January 1st - and add the number of days to get to the x-th one.
Hope it helps.
Add this after your code. There is the working fiddle https://jsfiddle.net/andreitodorut/ubLxy174/
var yearBegining = new Date();
yearBegining.setMonth(0,0);
yearBegining.setTime((yearBegining.getTime()/1000 + (86000 * day)) * 1000);
You can check the results with this content https://www.epochconverter.com/days/2018

How to get the week order number of a Date [duplicate]

This question already has answers here:
Show week number with Javascript?
(15 answers)
Closed 6 years ago.
All:
I wonder if there is a function in javascript that can get the week order of a Date, for example:
01/05/2016 is in the second week of this year, so the week order is 1(let start by 0)
Thanks
You can calculate the days pass from the begining of the year and calculate the number of weeks by deviding it to 7 (yhe number of the days in a week):
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
var week = Math.floor(day/7);
alert(week);

Get previous month date javascript [duplicate]

This question already has answers here:
How to get 30 days prior to current date?
(16 answers)
Closed 7 years ago.
How can I get previous month date in javascript. Suppose you have today's date like:
var abc = new date();
It will return today's date for example 03-11-2015. Now I want to get 03-10-2015. This is 30 days less than todays date. How can I do this?
var d = new Date();
d.setMonth(d.getMonth() - 1);
Check out momentjs, great little library for manipulating and formatting dates.
Complementing Robert Shenton's answer:
var d = new Date();
var newMonth = d.getMonth() - 1;
if(newMonth < 0){
newMonth += 12;
d.setYear(d.getFullYear() - 1); // use getFullYear instead of getYear !
}
d.setMonth(newMonth);

Calculate javascript date [duplicate]

This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 8 years ago.
i have the date 2013-12-28 and i want add one or more more day to it. so if i add one more day it will be 2013-12-29.
i try to add it by adding the value of it's date (date 28+1), it works, but what if i add 7 more day to it? the date will be 35, and of course it is not a valid date format.
can someone help me?
here's the example of my script:
var d = new Date();
var Y = d.getFullYear();
var M = d.getMonth()+1;
var D = d.getDate();
var DT = d.getDate()+1;// what if i + 20 days from today? the format would be invalid
var today = Y+"-"+M+"-"+D;
var tomorrow = Y+"-"+M+"-"+DT;
alert(today+" <> "+tomorrow);
// "<>" means nothing
You may try like this using getdate(), setdate() and getdate():
var myDate = new Date();
myDate.setDate(myDate.getDate() + 7);
If you already have a date object as in the code you show:
var d = new Date();
...then you can add 7 days to it like this:
d.setDate( d.getDate() + 7 );
...and it will automatically increment the month if needed.
Further reading:
The Date object
.getDate() method
.setDate() method
If you need to extract the year, month and day in order to format the result a particular way do so after adding days.
The solution is to convert your date string into unix timestamp, and them add 3600 * 24 * <number of days> to the timestamp and them convert it back to date string.
The code can be as follows:
function addDaysToDate(date, days) {
var time = Date.parse(date) + days * 24 * 3600;
date = new Date(time);
return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
}
var date = '2013-12-28';
console.log(addDaysToDate(date, 7));

Categories