how to get day format from new Date in Angularjs - javascript

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

Related

How to get next two days date (day after tomorrow's date) a value using JavaScript

const today = new Date()
const tomorrow = new Date(today)
const newDate = tomorrow.setDate(tomorrow.getDate() + 2)
console.log(newDate.toLocaleDateString('en-us'))
I'm trying to get next 2 days date with mm/dd/yyyy format, but getting issues.
I tried with following code:
console.log(new Date().toLocaleDateString('en-US'));
Example: today's date >> 6/1/2022
Expected result : 6/3/2022
Your problem comes from the fact, .setDate does not return a Date Object, but instead modifies the Date Object you call it on.
This means, tomorrow will be modified by calling .setDate.
Just change your code to the following, to get the expected result:
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 2)
console.log("today:", today.toLocaleDateString('en-US'))
console.log("in two days:", tomorrow.toLocaleDateString('en-us'))

React Native new Date() not working

I tried to get Date from datestring but it's not wokring properly.
var time = new Date('2017-12-26T02:12:00')
But when I called time.getHours() it returns 12.
I am not sure what I am doing wrong.
Use below code to get day, month and year.
var date = new Date(),
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear(),
today = day+"-"+month+"-"+year;
You can change the format of date as per your requirement.
Adding below code fixed the problem
time.setMinutes(time.getMinutes() + time.getTimezoneOffset());
:)

Datetime diff in minutes

I have 2 DateTime field in a form, and I want the difference between these 2 fields in minute.
I tried to parse DateTime into Date but it's not working :
<script>
$(document).ready(function () {
$("#mybundle_evenement_button").click(function () {
var field1 = $("#mybundle_evenement_debut").val();
var field2 = $("#mybundle_evenement_fin").val();
var date1 = new Date(field1);
var date2 = new Date(field2);
alert(date1);
});
});
</script>
If I alert() date1, it shows Invalid Date.
But if I alert() field1, it shows 15/09/2017 13:32 (format is : days/months/year hour:minutes).
Is it possible that new Date(field1) isn't working because of the format ?
I know that if I succeed to parse DateTime into Date, it'll be easy to have the difference in minutes, but I don't understand why it says Invalid Date.
dd/MM/yyyy HH:mm isn't a valid date format for Date.parse()
You have to format your date to a valid Date Time String Format, for example:
var field1 = $("#mybundle_evenement_debut").val();
var ISODate1 = field1.replace(/(\d+)\/(\d+)\/(\d+)/, "$3-$2-$1")
var date1 = new Date(ISODate1);
alert(date1) // => Fri Sep 15 2017 13:32:00 ...
The problem is about the format you are getting the date from the field. new Date() don't accepts this format. I think the best is to parse the string yourself. If the format is always the same just use new Date(year, month, day, hours, minutes, seconds, milliseconds).
var day = field.splice(0,2); field.splice(0,1);
var month = field.splice(0,2); field.splice(0,1);
var year = field.splice(0,2); field.splice(0,1);
var hour = field.splice(0,2); field.splice(0,1);
var minute = field.splice(0,2);
It's depend on your browser. I'll suggest to use the standard format is '2013/12/09 10:00'.
Okay! come to the point. You need to manually format the date from my latest answer regarding this same kind of issue. Please take a look at this link : Stange javascript Date behaviour on particular dates
And you could try this below code for getting the date difference in minutes.
var startTime = new Date('2013/12/09 10:00');
var endTime = new Date('2014/12/09 10:00');
var difference = endTime.getTime() - startTime.getTime();
var result = Math.round(difference / 60000);
alert(result);

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);

How to subtract two dates in javascript?

I want to get the difference between tow dates in javascript but my problem is that the first date is the currenet date of the PC :
today = new Date();
and the other date is a text format date like this for example:
other_date = '15.11.2013';
I want the today date to be same format as other_date and then subtract them , How can I change the format of the today to make the same as other_date ? and How I can make them both as date format to subtract them and get the difference correctly???
I would simply use the split function to get the date parts of the date string:
var today = new Date();
var other_date = '15.11.2013';
var dateparts = other_date.split('.');
var otherDate = new Date(dateparts[2], dateparts[1]-1, dateparts[0]); // substract 1 month because month starting with 0
var difference = today.getTime() - otherDate.getTime(); // difference in ms

Categories