javascript: fetch day of the week based on a specific date - javascript

I searched stackoverflow but did not find a solution to my problem.
I have a specific date say 2014-05-20 and I want to get the Day of the week for the mentioned date.
I tried the following
var date = new Date();
console.log(date.getDay());
But this return the Current Day. What I require is the day based on the given date!
I also tried
var givendate = '2014-05-20';
new Date(givendate)
But the above does not generate anything.

Your question both code you combine and check answer is get on yourself without my answer still i show you code,
Check this Demo jsFiddle
JavaScript
var givendate = '2014-05-20';
var date = new Date(givendate);
console.log(date.getDay());
Console log
2

you may try
var date = new Date(2014, 05, 20).getDay(); // Date(year, month, date)
console.log(date);
you will get integer 0 for sunday - 6 for saturday

Related

unable to get previous date using moment.js

I have the following date
let api_date = '2022-03-01T00:00:00.000Z'
Now, i want to get previous 2 dates starting from api_date. Basically now i need dates as 2022-02-28T00:00:00.000Z and 2022-02-27T00:00:00.000Z
Basically for api_date of 1st March, i need previous 2 dates as Feb 28th and Feb 27th.
I tried using this below code
let t-1 = moment().substract(1, 'days')
let t-2 = moment().subtract(2, 'days')
But this only provides previous 2 dates from the present date. i.e. present date is 2nd March, so it provides previous 2 dates based of 2nd March.
how can i use moment to get my current specified date and get previous 2 dates based on that. Any advice to achieve that ? i saw the documentation of moment.js too but i didnt find a definitive answer.
You should create a date object from the date you wanna parse like this,
let api_date = '2022-03-01T00:00:00.000Z'
var dateObj = new Date(api_date);
then you can create a moment object based on the date object just like this,
var momentObj = moment(dateObj);
then if you perfom your specific logic you will get your desired result, like this
let date1 = momentObj.substract(1, 'days')
let date2 = momentObj.substract(2, 'days')
You were almost there, you need to pass your date to moment. Otherwise it defaults to current date ( as you found out ).
let t-1 = moment(api_date).subtract(1, 'days')
let t-2 = moment(api_date).subtract(2, 'days')

Google spreadsheet script to calculate the last weekday

I've a Google sheets script where I try to find yesterday's date as a workday by subtracting 1 from today's date.
//Get today's date
var today = new Date();
//Take today's date and subtract 1 making yesterday's date
var yesterday = new Date(today.getTime()-1 * (24*3600*1000));
The problem I face is that on a Monday it will return a Sunday, rather than a Friday.
I tried to use the Workday function to fix the issue but I am getting some not defined errors.
var yesterday = new Date(WEEKDAY((today.getTime()-1 * (24*3600*1000)));
What would be the best method for fixing the issue?

Get MongoDB items matching date

I am trying to get a count on posted item from meteor-mongo matching date periods.
I inserted my posts dates as such.
posts
submitted: new Date()
the dates in the database have the following format.
yyyy-mm-dd 16:16:34.317Z // I do not understand the last part (what format it is)
I have tried this to get match the date of today from the submitted field
var currentDate = new Date();
var dd = currentDate.getDate();
var mm = currentDate.getMonth()+1;
var yyyy = currentDate.getFullYear();
var today = yyyy+'-'+mm+'-'+dd;
Posts.find({submitted: today}).count()
However, the last part is returning 0.
Is it because the last hh,mm,ss part of today is missing? If so, how can I tell meteor-mongoto ignore the time part of date so that I can return that count?
I don't like to deal with JS date objects formats, and i guess you either (I do not understand the last part (what format it is))
Give a try to momentjs package, the documentation its pretty clear.
So on the insert you can have something like.
var today = moment().format(''MMMM Do YYYY, h:mm:ss a'); // April 3rd 2015, 12:17:06 pm
Posts.insert({subbmited:today})
and do a simple find like this.
var endQuery = today..add('days', 3).calendar(); // just an example.
Posts.find({submitted: {$gt: today,$lt:endQuery}})
Here thanks to #Larry Maccherone point we are using $gte (grater than) and $lt (less than), so this works like find me post between the post submitted day and the post submitted dat + 3 days (like you ask managing ranges)
You are storing your submitted date in MongoDB with both time and timezone information. Performing a direct comparison of your currentDate with the submitted date in Mongo will never be true, since your currentDate does not contain time information.
Also you are using a String data type to query the date, which also will not work since you need to use a Date data type. Something like this will work for you:
var today = new Date();
today.setHours(0, 0, 0, 0);
Posts.find({submitted: {$gt: today}})
Which will return all the posts with a date greater than midnight of today's date.
Try something like this
posts.find({"submitted": /.*today*/})

Milliseconds for current date in javascript

When I try the following
new Date().valueOf()
the result is 140082670954. For
new Date('05/23/2014').valueOf()
the result is 1400783400000.
There is a difference in the millisecond outputs. The second one is at 00:00:00 hrs but the first one is at 12pm with todays date.
I need to get the milliseconds as in the second one. How would I do this dynamically?
When you do:
new Date()
a new Date object is created with a time value for the current instant. When you do:
new Date('05/23/2014')
a new Date object is created at 00:00:00.000 on the specified date. If you want the equivalent using the constructor, then create the Date and set the time appropriately:
var d = new Date();
d.setHours(0,0,0,0);
NB
Please don't pass strings to the Date constructor. It calls Date.parse which is largely implementation dependent and inconsistent across browsers (even using the string format specified in ES5). Call the constructor with the required values:
new Date(2014, 4, 23);
noting that months are zero indexed so May is 4.
I take it that you want the milliseconds from midnight of a given day? I am afraid it won't be too simple, thanks to JavaScript's very constrained date-time API. You can extract the year, month, and day from the date object and create a new object from those:
var now = new Date();
var day = now.getDate();
var month = now.getMonth();
var year = now.getFullYear();
var today = new Date(year, month, day);
var millis = today.valueOf();
BTW: You mention that the first one "is at 12pm" - that depends on at what time you execute the statement. new Date() gives you the date including the current local time. So it appears that you tried it at roughly 12pm :-)

JavaScript date math not working

I have searched this forum and found many useful answers, but one of the answers that I used only works under certain conditions.
I am populating a week calendar, and simply need to determine the start of the week (Monday) from a Date picker, and then I add to that date to populate text fields with the following 6 days. This works only if the date picker selection is in the same month.
So, if I select Wednesday May 15th 2013, it correctly returns and populates the Monday with May 13, the Tuesday with May 14, etc.
But, if I select Wednesday May 1, 2013, it correctly populates Monday Apr 29, but Tuesday it puts as May 30 (adding a month instead of a day).
I should note that I am building this in Application Craft, so I don't know if that has any impact.
Here's my code:
var curr = new Date(app.getValue("DatePicker2")); // get selected date
var first = curr.getDate() - curr.getDay() +1; // Adjust for monday start of week
var firstday = new Date(curr.setDate(first));
var secondday = new Date();
secondday.setDate(firstday.getDate()+1);
Can anyone see where I have gone wrong?
Thanks
Tammy
Here
secondday.setDate(firstday.getDate()+1)
since you are specifying only the date in the setDate function, it would assume "this" month which happens to be May in this case
So, you can do
secondday = new Date(firstday.getFullYear(), firstday.getMonth(), firstday.getDate()+1)

Categories