set midnight in javascript for cookie expiration - javascript

Good morning, I currently have this script:
now = new Date (). getTime ();
now = now + 86400000;
this takes the current date and time and adds 24 hours to it.
Instead, I would need to set midnight of the current day. I tried with:
var d = new Date ();
date = d.setHours (0,0,0,0);
now = date / 1000;
but it does not work! I need this to set the expiration of a cookie:
$ .cookies.set ('mycookie2250', 'true', {expiresAt: new Date (now)});
in the first case it works but in the second it doesn't.
Where am I wrong?

var date = new Date();
date.setHours(0,0,0,0);
// date is midnight.

Related

Material2 Datepicker - Convert date to timestamp without time [duplicate]

Can I convert iso date to milliseconds?
for example I want to convert this iso
2012-02-10T13:19:11+0000
to milliseconds.
Because I want to compare current date from the created date. And created date is an iso date.
Try this
var date = new Date("11/21/1987 16:00:00"); // some mock date
var milliseconds = date.getTime();
// This will return you the number of milliseconds
// elapsed from January 1, 1970
// if your date is less than that date, the value will be negative
console.log(milliseconds);
EDIT
You've provided an ISO date. It is also accepted by the constructor of the Date object
var myDate = new Date("2012-02-10T13:19:11+0000");
var result = myDate.getTime();
console.log(result);
Edit
The best I've found is to get rid of the offset manually.
var myDate = new Date("2012-02-10T13:19:11+0000");
var offset = myDate.getTimezoneOffset() * 60 * 1000;
var withOffset = myDate.getTime();
var withoutOffset = withOffset - offset;
console.log(withOffset);
console.log(withoutOffset);
Seems working. As far as problems with converting ISO string into the Date object you may refer to the links provided.
EDIT
Fixed the bug with incorrect conversion to milliseconds according to Prasad19sara's comment.
A shorthand of the previous solutions is
var myDate = +new Date("2012-02-10T13:19:11+0000");
It does an on the fly type conversion and directly outputs date in millisecond format.
Another way is also using parse method of Date util which only outputs EPOCH time in milliseconds.
var myDate = Date.parse("2012-02-10T13:19:11+0000");
Another option as of 2017 is to use Date.parse(). MDN's documentation points out, however, that it is unreliable prior to ES5.
var date = new Date(); // today's date and time in ISO format
var myDate = Date.parse(date);
See the fiddle for more details.
Yes, you can do this in a single line
let ms = Date.parse('2019-05-15 07:11:10.673Z');
console.log(ms);//1557904270673
Another possible solution is to compare current date with January 1, 1970, you can get January 1, 1970 by new Date(0);
var date = new Date();
var myDate= date - new Date(0);
Another solution could be to use Number object parser like this:
let result = Number(new Date("2012-02-10T13:19:11+0000"));
let resultWithGetTime = (new Date("2012-02-10T13:19:11+0000")).getTime();
console.log(result);
console.log(resultWithGetTime);
This converts to milliseconds just like getTime() on Date object
var date = new Date()
console.log(" Date in MS last three digit = "+ date.getMilliseconds())
console.log(" MS = "+ Date.now())
Using this we can get date in milliseconds
var date = new Date(date_string);
var milliseconds = date.getTime();
This worked for me!
if wants to convert UTC date to milliseconds
syntax : Date.UTC(year, month, ?day, ?hours, ?min, ?sec, ?milisec);
e.g :
date_in_mili = Date.UTC(2020, 07, 03, 03, 40, 40, 40);
console.log('miliseconds', date_in_mili);
In case if anyone wants to grab only the Time from a ISO Date, following will be helpful. I was searching for that and I couldn't find a question for it. So in case some one sees will be helpful.
let isoDate = '2020-09-28T15:27:15+05:30';
let result = isoDate.match(/\d\d:\d\d/);
console.log(result[0]);
The output will be the only the time from isoDate which is,
15:27

JavaScript Date object in another time zone, how to render office hours? [duplicate]

This question already has an answer here:
new Date() for a specific timezone in JavaScript
(1 answer)
Closed 5 years ago.
This seemed fairly trivial but I might be over-thinking it.
I would like to render my chat widget between 9AM(PST) and 5PM(PST) mon-fri
Using new Date() always puts time into the browsers time zone. Basically i need to instantiate a date in PST and check if between days and hours.
var d = new Date();
var day = d.getDay();
var hour = d.getHours();
if (day > 0 && day < 6 && hour > 9 && hour < 17) {
renderChatWidget($('#chat-widget')):
}
I think this is incorrect because it uses the browser time, so if its 9:30AM in London then PST time would be like 2am and it would still render the chat widget...
function calcTime(city, offset) {
// create Date object for current location
var d = new Date();
// convert to msec
// subtract local time zone offset
// get UTC time in msec
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
var nd = new Date(utc + (3600000*offset));
// return time as a string
return "The local time for "+ city +" is "+ nd.toLocaleString();
}
document.getElementById('title').innerHTML=(calcTime('California', '-7'));
<h1 id="title">Time Zone Example</h1>
This is from this post and I just turned it into a snippet to see it work. You can then use the modified (correct) date through your tests using the correct time zone.
You can use UTC time.
var current_time = new Date;
var utc_time = Date.UTC(
current_time.getUTCFullYear(),
current_time.getUTCMonth(),
current_time.getUTCDate() ,
current_time.getUTCHours(),
current_time.getUTCMinutes(),
current_time.getUTCSeconds(),
current_time.getUTCMilliseconds());

how to get day format from new Date in Angularjs

i want to get the value of the day format from new Date()(current date) in my angularjs projet. I try this code in my javascript file:
var today = (new Date()).toISOString();
console.log(today.getDay());
when running my code, i get this message error :
TypeError: today.getDay is not a function
however there are many solutions with this syntax.
How can i fix it please. Any help is appreciated
Use getDay on the Date object not on the ISO string:
var today = (new Date()).getDay();
getDay returns a value from 0(Sunday) to 6(Saturday).
If you want current date and day according to your timezone then ->
var today = new Date().getDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getDate()
If you want current date and day according to UTC timezone then ->
var today = new Date().getUTCDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getUTCDate()
You can get date by using below code
let dayno = new Date(this.date.getFullYear(), this.date.getMonth() ,20).getDay();<br>
if date is 20-11-2019 then Day No is :3

How can I get todays timestamp at 4pm?

I am looking for a way to receive the timestamp of today's 4pm in JavaScript.
When I use this code
Math.floor(Date.now() / 1000);
it give me the current timestamp. How can I specify just the hour to be static, the rest to rely on the current day?
You can do this by getting the current DateTime and change the hour, code:
var d = new Date();
d.setHours(16,0,0,0);//The setHours method can take optional minutes, seconds and ms arguments, but you can also do setHours(16)
Math.floor(d / 1000);
Here is the code to do that:
var d = new Date();//stores current date time
d.setHours(16);//4pm = 16 in 24 hour time
Now when you'll type console.log(d), you'll get the following required result.
Tue May 12 2015 20:58:24 GMT+0500 (PKT)
Try:
var date = new Date();
date.setHours(16);
date.setMinutes(0);
date.setSeconds(0);
Math.floor(date.getTime() / 1000);
Instantiate a Date object and set the hours like so:
var d = new Date;
d.setHours(16);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

How to construct javascript date object from date and time string

I have a date string like 08/27/2014 and time string like 18:29 . I want to convert it into javascript Date object.
Previously i was only concerned about the date so i was doing
var date = $.datepicker.parseDate('mm/dd/yy', '08/27/2014');
But now i am concerned about the time also. How can i include time now.?
I did something like this now
var d_p = $('#dt').val().split('/');
var t_p = $('#tt').val().split(':');
var date = new Date(d_p[2], d_p[0], d_p[1], t_p[0], t_p[1], 0, 0);
But looks ugly..
var myDate = new Date('2014-08-27T18:29:00');
alert(myDate);
This is Date constructor:
new Date(year, month, day, hours, minutes, seconds, milliseconds);
How about this:
var date = $.datepicker.parseDate('mm/dd/yy', '08/27/2014');
date.setHours(18).setMinutes(29);

Categories