web api not receiving date from angularjs - javascript

Hi I am developing web application in angularjs. I have one date in angularjs for example 13-10-2017. In c# i have below field
public DateTime LicenseExpiryDate { get; set; }
When i send 13-10-2017 in ajax request,LicenseExpiryDate accepts as 0001-01-01. May i know how to fix this? I can see my angular date is in dd-mm-yy format and c# date default format is yyyy-mm-dd.
i tried to convert it to yyyy-mm-dd as below
function formatDateDOsetyymmdd(date) {
debugger;
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year,month, day].join('-');
}
and this returns NaN-NaN-NaN
Any help would be appreciated. Thank you.

You are getting a string in form dd-mm-yyyy on input, what you want to do is turn that string into three numbers, and use those in the new Date call
function formatDateDOsetyymmdd(date) {
date = date.split('-').map(Number);
var d = new Date(date[2], date[1] - 1, date[0]),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year,month, day].join('-');
}
console.log(formatDateDOsetyymmdd('13-10-2017'));

you can use moment.js , if you can't do this in JavaScript means you can use it.
https://momentjs.com/

Related

How to get tomorrow's date of a specific date

I am trying to get tomorrow's date of a specific date using JavaScript in format (yyyy-mm-dd). For example the specific date is 2021-08-31 and I have got this script:
var date = "2021-08-31"
date = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])
date.setDate(date.getDate() + 1);
var tomorrows_date_month = date.getMonth()
var tomorrows_date_day = date.getDate()
var tomorrows_date_year = date.getFullYear()
console.log(tomorrows_date_year + "-" + tomorrows_date_month + "-" + tomorrows_date_day)
The expected output is:
2021-09-01
But the output of this code is :
2021-9-2
First you don't need split "2021-08-31" to use as date parameter, so just use new Date("2021-08-31");
Second note that you need to use d.getMonth() + 1 and add leading zero if the length is less than 2:
Try this one:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = "2021-08-31"
var date1 = new Date(date);
console.log(formatDate(date1.addDays(1)));
Internally js month is stored as a value between 0 and 11. So you need to minusdate.split("-")[1] by 1. Otherwise, javascript will think that your month is actually September and we know that "2021-09-32" is translated to "2021-10-2", therefore the date is shown as "2".
var date = "2021-08-31"
date = new Date(date.split("-")[0],date.split("-")[1] - 1,date.split("-")[2])
date.setDate(date.getDate() + 1)
var tomorrows_date_month = date.getMonth() + 1
var tomorrows_date_day = date.getDate()
var tomorrows_date_year = date.getFullYear()
console.log(tomorrows_date_year + "-" + tomorrows_date_month + "-" + tomorrows_date_day)
Also note that date = new Date("2021-08-31") is enough for converting a string into a Date object.
new Date(new Date(date + 'T00:00Z').getTime() + 86400000).toISOString().substr(0, 10)
The added 'T00:00Z' assures the date is parsed as UTC, to match the UTC timezone used by toISOString(). Adding 86400000 (the number of milliseconds in one day) advances the date without having to fuss with the date field directly.

How to format Date Time in JavaScript? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I have a date timestamp coming in this format 2021-02-08T19:36:47.000+0000. How can I format this in JavaScript in DD-MM-YYYY Hh:mm format.
Download moment.js and include to your file
then you can use like this
moment("2021-02-08T19:36:47.000+0000).format("DD-MM-YYYY Hh:mm")
I usually use this in my every project
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js
This is plain Javascript
// 2021-02-08T19:36:47.000+0000
function formatDate(date) {
var d = new Date(date),
minutes = ':' + d.getMinutes(),
hours = ' ' + d.getHours(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = '-' + d.getFullYear();
if (month.length < 2) {
month = '-0' + month;
} else if (month.length < 3) {
month = '-' + month;
}
if (day.length < 2)
day = '0' + day;
// DD-MM-YYYY HH:mm
return [day, month, year, hours, minutes].join('');
}
console.log(formatDate("2021-02-08T19:36:47.000+0000"))

Javascript Text Field / Date Parser

I am trying to a add a parser to a text field to make sure the user enters a date with the format MM/DD/YYYY. I am also using a Calendar Picker to manipulate state as well, and updating the text-box with moment.js ... however, I need to add a parser for when the user is typing the date, where they can type the / but also the / adds itself when needed.
A few scenarios with a before/after idea of parser:
3 -> 03/
3161995 -> 03/16/1995
31/ -> 03/01/
you can make use following function
function formatDate(date) {
let d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [month, day, year].join('-');
}
/* pass the data as follows
console.log(formateDate(new Date())); */

change date format in displaying

I have this below code where i am trying to alert the date selected on click of an image. I am getting the alert but the issue is date format.
Below is alert i am getting. I need the alert in mm/dd/yyy.
And also why i am getting Thu Jan 01 1970 if i use todatestring() method.
Below is the javascript code
Try this function to return mm/dd/yyyy
function getmmDdYy(thisDate) {
return ('0' + (thisDate.getMonth() + 1)).slice(-2) +
'/' +
("0" + (thisDate.getDate())).slice(-2) +
'/' +
thisDate.getFullYear();
}
So on your fnUpdateDate(), do this:
function fnUpdateDate(date1,date2){
alert(getmmDdYy(new Date(date1)) +" ss "+ getmmDdYy(new Date(date2)));
}
Let me know if this works.
You can retrieve the single components from a date (day, month, year) and build a string with them (https://stackoverflow.com/a/30272803).
If you add the library moment.js you will be able to use formatting strings like YYYYMMDD (https://stackoverflow.com/a/27635223).
You can pass your date to this function and return the formate as per your need
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return (month+"/"+day+"/"+year);
}

Convert date to ccyymmdd using javascript

I need to use JavaScript to covert a date to to from '2012-09-15T00:00:00' to a CCYYMMDD format? How can I do this?
I don't see what .NET and XSLT have to do with your question.
You could use the Date constructor to parse the ISO 8601 encoded string into a javascript Date object:
var dateStr = '2012-09-15T00:00:00';
var date = new Date(dateStr);
and then build the desired format:
var year = '' + date.getFullYear();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month : month;
var day = '' + date.getDate();
day = day < 10 ? '0' + day : day;
var formattedDate = year + month + day;
And here's a live demo.
Using replace and split.
var date = '2012-09-15T00:00:00';
date = date.replace(/-/,"").split("T")[0];// date will be 20120915

Categories

Resources