How to get last day of previous month using current date? - javascript

I'm trying to get the last of day of previous month using the current date:
var myDate = new Date();
According to MDN:
if 0 is provided for dayValue, the date will be set to the last day of the previous month.
But when set date to zero:
myDate.setDate(0)
console.log(JSON.stringify(myDate));
I get "2021-08-01T01:18:34.021Z" which first day of the current month. What is wrong with this approach?

JSON.stringify() is serializing the timestamp with a Z timezone, indicating UTC. The difference between UTC and your local timezone is causing the date to rollover to the next day.
You can use toLocaleString() to print the date in your local timezone:
var myDate = new Date();
myDate.setDate(0);
console.log(myDate.toLocaleString());

I would use dateInstance.toString() or dateInstance.toLocaleString():
const myDate = new Date;
myDate.setDate(0); myDate.setHours(0, 0, 0, 0);
console.log(myDate.toString()); console.log(myDate.toLocaleString());

You can use date-fns package
var df = require("date-fns")
let myDate = new Date() //Thu Aug 05 2021 22:16:09
let lastDayOfPrevMonth = df.endOfMonth(df.subMonths(myDate, 1)) //Sat Jul 31 2021 23:59:59 GMT-0400

Related

javascript tomorrow's date and 00:00 time

So I'm trying to get add 1 day to today's date (tomorrow), but instead of having it it exactly 24 hours from when the query is run, I want it to be basically tomorrow morning 00:00..
eg if it's currently Tue Jun 23 2020 13:01:57 GMT+0200 when I run new Date(), I need to get Wed Jun 24 2020 00:00:00 GMT+0200 to post to server (using axios).
Here's what I've tried, but it returns todays date at midnight.
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(new Date().getDate() + 1);
let payload = {
available_from: tomorrow.setHours(0,0,0,0),
}
axios.post(url, payload)......
Any ideas?
You can just use Date.setHours to set the time on today's date to 24:00:00.000 i.e. tomorrow morning at midnight.
const tomorrow = new Date();
console.log(tomorrow.toString());
tomorrow.setHours(24, 0, 0, 0);
console.log(tomorrow.toString());

Is there an external api to get the current date/time in British Summer Time format

I have the following javascript function, where i am getting 2 variables from our system (expireddate + modifieddate)+ i am getting the current Date, as follow:-
function(){
var expirydate = item.get_item('ExpireDate');
var modifieddate = item.get_item('Modified');
var currentdate = new Date();
now in my case all the variables will be in this format Tue Jun 11 2019 00:00:00 GMT+0100 (British Summer Time).. and i am not facing any issue since my pc is also using the (British Summer Time) date, so the var currentdate = new Date(); will be compatible with other formats... but my question is how i can guarantee that var currentdate = new Date(); will always be in the (British Summer Time) date regardless of the users' PCs settings? so if the user is using another date format, to have the var currentdate = new Date(); in (British Summer Time) date format ? can i for example get the current date from external source?
I think I understand what you are looking for, and if so, you will want to use toLocaleString(), and tell it what format you would like the result to be in.
The following will result in british time format in the UTC timezone.
let date = new Date()
console.log('UTC:', date.toLocaleString('en-GB', { timeZone: 'UTC' }))
console.log('Guernsey:', date.toLocaleString('en-GB', { timeZone: 'Europe/Guernsey' }))
console.log('Guernsey Time:', date.toLocaleTimeString('en-GB', { timeZone: 'Europe/Guernsey' }))

get the current date from GMT

I want to get the date from the GMT time but it returns the date which is one day ahead. How can I get the date mentioned in the GMT string always?
new Date("Mon, 27 Aug 2018 22:00:00 GMT").getDate()
This command returns 28 as the output, but I want 27.
Is there anything I need to add?
Thanks in advance.
Try this One.I think your problem will be solved.
<script>
function myFunction() {
var d = new Date();
var n = d.getUTCDate();
document.getElementById("demo").innerHTML = n;
}
</script>
When you create a new Date() the browser returns date based on your device timezone. You can use Date.getTimezoneOffset() to get GMT offset time difference and then adjust the time by multiplying the value.
// Your date
var myDate = new Date("Mon, 27 Aug 2018 22:00:00 GMT")
// Convert your date to local using getTimezoneOffset() and multiply with 60000 to get time adjusted GMT 0
var myDateLocal =new Date( myDate.valueOf() + myDate.getTimezoneOffset() * 60000 ).getDate();
document.getElementById("myDate").innerHTML=myDateLocal;
<h1 id="myDate" ></h1>

Javascript setting date for mongodb

I'm trying to set my Date object in javascript, but I keep getting wrong date. This is how I set
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth();
var day = today.getDate();
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var create_date = new Date(year,month,day,0,0,0).toISOString();
My local datetime is 2015-11-17 21:00:00, however this creates 2015-11-16 22:00:00.000Z. If I use hour, minute and second variables in date constructor it creates the correct time in mongodb. I want to set date as 2015-11-17 00:00:00. What might be the problem?
Thank you
EDIT
Even the date is set correct in javascript, date in mongo db is seen as 2015-11-16 22:00:00.000Z
To set the date to the start of today, use the setHours() method of the Date object as follows:
var create_date = new Date();
create_date.setHours(0,0,0,0);
console.log(create_date); // prints Tue Nov 17 2015 00:00:00 GMT+0000 (GMT Standard Time)
For UTC, use setUTCHours():
create_date.setUTCHours(0,0,0,0);

Javascript not returning the month from unix timestamp

I have a UNIX timestamp 1411866803 which is equivalent to Sun, 28 Sep 2014 01:13:23 GMT
I want to get the month from the timestamp according to local time of the user. I use the following function:
<script>
var date = new Date(1411866803);
var month = date.getMonth();
alert(month);
</script>
This should return 9 instead it returns 0. Is there something wrong with the above syntax? Thanks :)
JavaScript timestamps are expressed in milliseconds. Multiply by 1000:
var date = new Date(1411866803000);
var month = date.getMonth();
alert(month); // => 8
Also, months are 0-based, so September is 8, not 9.
The Date object takes in milliseconds, whereas UNIX timestamps use seconds. You have to convert it over.
var date = new Date(1411866803*1000);
var month = date.getMonth();
alert(month);
The Date object takes as milisecond instead of seconds. So, just put three zeroes and you will be fine.
var date = new Date(1411866803000);
var month = date.getMonth();
alert(month);
If I test your script in a console, I get this date to be:
var date = new Date(1411866803)
Date {Sat Jan 17 1970 20:11:06 GMT+1200 (NZST)}
And according to that page : http://www.w3schools.com/jsref/jsref_getmonth.asp
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
Note: January is 0, February is 1, and so on.

Categories