I have a problem with show Date from database SQL Server in JavaScript.
My JavaScript:
$.each(data, function (i, dt) {
var date = new Date(dt.Date);
alert(date);
});
I want to show '2023-02-02' but alert show /Date(1675270800000)/. What should I type in my code?
I was trying to use:
var date = new Date(dt.Date);
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
alert(day +"" + month + "" + year);
but the alert show NaNNaNNaN
I primo in the console tried your code:
var date = new Date(1675270800000);
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
alert(day +"-" + month + "-" + year);
And I got "2-2-2023".
Related
How to calculate the date and change the date format.
I can't compare the date code below. I want to know why? I want an example to compare date or calculate day results of different days
I want to change format before and after that comparing date (formatDate: dd/mm/yyyy)
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var d1 = curr_date + "/" + curr_month + "/" + curr_year;
//alert(d1); change format date completed 12/04/2019
var date2 = new Date("04/12/2018");
var curr_date2 = date2.getDate();
var curr_month2 = date2.getMonth() + 1;
var curr_year2 = date2.getFullYear();
var d2 = curr_date2 + "/" + curr_month2 + "/" + curr_year2;
//alert(d2); // change format date completed 12/04/2018
if (d1 > d2) {
console.log("aaa");
} else {
console.log("bbb");
}
There are no issues with the first part of your code when you are converting them from JavaScript Date objects to the mentioned date formats.
However, when it comes to comparing the dates, you should use getTime() on the Date objects.
if(d.getTime() > date2.getTime()) {
console.log("aaa");
}else{
console.log("bbb");
}
What you tried to do on your code is to compare d1 and d2, which is wrong because they are both strings, and will likely give you the wrong solution.
Here is the full code. Basically, you only need to change the last chunk of your code with that if.. else statement.
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var d1 = curr_date + "/" + curr_month + "/" + curr_year;
console.log(d1);
//alert(d1); change format date completed 12/04/2019
var date2 = new Date("04/12/2018");
var curr_date2 = date2.getDate();
var curr_month2 = date2.getMonth() + 1;
var curr_year2 = date2.getFullYear();
var d2 = curr_date2 + "/" + curr_month2 + "/" + curr_year2;
console.log(d2);
//alert(d2); // change format date completed 12/04/2018
console.log(d.getTime())
console.log(date2.getTime())
// only change the below
if(d.getTime() > date2.getTime()) {
console.log("aaa");
} else {
console.log("bbb");
}
I am trying to get the day 90 days after today. This is my code:
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
When I print threeMonthsFromToday, I get the correct date: 2017-04-24T15:17:42.641Z. However, when I try to reformat the date to be in the form dd/mm/yyyy using this code:
var day = ('0' + threeMonthsFromToday.getDate()).slice(-2);
var month = ('0' + threeMonthsFromToday.getMonth() + 1).slice(-2);
var year = threeMonthsFromToday.getFullYear();
var date = day + '/' + month + '/' + year;
I get a completely different and invalid date: 24/31/2017.
I have been debugging this for hours and still can't seem to figure what I am doing wrong.
Well, '0' + threeMonthsFromToday.getMonth() give you a string : "03" then you add 1 converted to string giving you "031" for month before slice.
Use this :
var month = ('0' + (threeMonthsFromToday.getMonth() + 1)).slice(-2);
You are missing the basic BODMAS rule here please modify your code as follows
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
var day = ('0' + threeMonthsFromToday.getDate()).slice(-2);
var month = ('0' + (threeMonthsFromToday.getMonth() + 1)).slice(-2);
var year = threeMonthsFromToday.getFullYear();
var date = day + '/' + month + '/' + year;
the operations are performed from left to right, so month is getting converted to string before being added to a number. Including a bracket will first perform operation inside bracket and then make it a string
Can you use toLocaleString?
threeMonthsFromToday.toLocaleDateString('en-GB')
Below does the trick for you ...
getMonth() +1 before adding the "0" to it so that you get an arithematic +1
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
var day = ('0' + threeMonthsFromToday.getDate()).slice(-2);
var month = ('0' + (threeMonthsFromToday.getMonth()+1)).slice(-2);
var year = threeMonthsFromToday.getFullYear();
var date = day + '/' + month + '/' + year;
console.log(date);
This should work.
var day = threeMonthsFromToday.getDate()
if(day < 10){
day = '0' + day
}
var month = threeMonthsFromToday.getMonth()+1
if(month<10){
month = '0' + month
}
var year = threeMonthsFromToday.getFullYear()
var date = day + '/' + month + '/' + year
Use Simple toLocaleDateString method
The toLocaleDateString() method returns a string with a language sensitive representation of the date portion of this date.
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
var date = threeMonthsFromToday.toLocaleDateString();
console.log(date);
//result in console : "24/04/2017"
Try it out on your console.
I'm new to javascript and I'm trying to generate a date that is:
A.) Formatted (mm/dd/yyyy)
B.) Displayed as three months later than the actual date.
I've spliced together a few things I could find and is seems to work to return the correct date, but I'm concerned it will break later in the year. Is there a better way to accomplish what I'm trying to do? The script is below, thanks in advance for the help.
<script type="text/javascript">
var dt = new Date();
var month = dt.getMonth();
var day = dt.getDate();
var year = dt.getFullYear();
if (month == 9) { var dt = new Date();
var month = dt.getMonth()-8;
var day = dt.getDate();
var year = dt.getFullYear();
var next_year = dt.getFullYear()+1;
document.write(month + '-' + day + '-' + next_year)}
else if (month == 10) { var dt = new Date();
var month = dt.getMonth()-8;
var day = dt.getDate();
var year = dt.getFullYear();
var next_year = dt.getFullYear()+1;
document.write(month + '-' + day + '-' + next_year)}
else if (month == 11) { var dt = new Date();
var month = dt.getMonth()-8;
var day = dt.getDate();
var year = dt.getFullYear();
var next_year = dt.getFullYear()+1;
document.write(month + '-' + day + '-' + next_year)}
else { var dt = new Date();
var month = dt.getMonth()+4;
var day = dt.getDate();
var year = dt.getFullYear();
document.write(month + '-' + day + '-' + year);}
</script>
The date object has methods that allows not only to set month, date and year, but get too.
function getMyDate() {
var date = new Date();
date.setMonth(date.getMonth() + 3);
return (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getFullYear();
}
document.write(getMyDate());
P.S. The scope of var is the function, not block {}. So, if you declared var dt at the top of your scope, you mustn’t use var dt in if statement. It’s a mistake.
You don't need to worry about dealing with the last three months of the year. Adding three months to a a date that causes it to 'overflow' will correctly set it to next year (eg adding three months to November 2015 will produce a date in February 2016).
var dt = new Date();
dt.setMonth(dt.getMonth() + 3);
document.write((dt.getMonth() + 1) + "-" + dt.getDate() + "-" + dt.getFullYear();
For what it's worth, your code looks correct. Just unnecessarily complex.
To print todays date you should concat the date values:
var d = new Date();
var str = (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
if you want to add months to your date:
var d = new Date();
var threeMonthsFromToday = new Date(new Date(d).setMonth(d.getMonth()+3));
var str = (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
I need DD-MM-YYYY Format of the given date.
var todaydate = /Date(1394908200000)/; //Serialize date
var date = eval("new " + todaydate.replace(/\//g, ""));
alert('date is :'+date)
but output look like,
date is :Wed Jun 11 2014 00:00:00 GMT+0530 (India Standard Time)
Expected output like,
date is :11-06-2014
Try this
var date = new Date(); //new Date(1394908200000)
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [day, mnth, date.getFullYear()].join("-");
}
var final = convert(date);
alert('date is :' + final)
DEMO
Besides of JS like other people mention, you can also use the .datepicker from jquery ui plug-in
var dt = $.datepicker.formatDate('dd-mm-yy', new Date(1394908200000));
alert(dt);
JSFiddle, use jquery ui plug-in
WORKING FIDDLE
Try this-
var date = new Date();
function myDateFormatter (dateobject) {
var d = new Date(dateobject);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var date = day + "-" + month + "-" + year;
return date;
};
var dateformat = myDateFormatter(date);
alert('date is :' + dateformat);
Like i need to get todays date in format like 20120924 (yyyymmdd).How can i get this in javascript.
You could add a method to the date prototype, so you can use it on any date object:
Date.prototype.toMyString = function () {
function padZero(obj) {
obj = obj + '';
if (obj.length == 1)
obj = "0" + obj
return obj;
}
var output = "";
output += this.getFullYear();
output += padZero(this.getMonth()+1);
output += padZero(this.getDate());
return output;
}
var d = new Date();
alert(d.toMyString()); // Today
var otherDate = new Date(2012,0,1);
alert(otherDate.toMyString()); //Jan 1 2012
Fiddle: http://jsfiddle.net/johnkoer/4rk7K/10/
This worked for me.
var rDate = (new Date()).toISOString().slice(0, 10).replace(/-/g, "");
Try this.
var date = new Date();
var year = date.getFullYear().toString();
var month = date.getMonth().toString();
var day = date.getDate().toString();
if (parseInt(month) < 10) month = "0" + month;
if (parseInt(day) < 10) day = "0" + day;
var parsedDate = year + month + day;
(edit)
Improved this function by making the day equate to the day of the month, rather than the day of the week.
How about
date = new Date().toJSON().substr(0,10).split("-")
date = date[0] + date[1] + date[2]
Edit:
This will return the UTC date, not local date...
For local date, you could use:
date = new Date().toLocaleDateString().split("/"); // "M/D/YYYY"
date[0] = date[0].length == 1 ? "0" + date[0] : date[0];
date[1] = date[1].length == 1 ? "0" + date[1] : date[1];
date = date[2] + date[0] + date[1];
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_year + curr_month + curr_date);
That should give the right date:)