change date format in displaying - javascript

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

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.

web api not receiving date from angularjs

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/

Javascript change the data in some data

I have a variable that's returning the following content:
{
"id":1,
"refnumber":3,
"active":false,
"date":"2017-09-14T23:00:00.000Z",
"addons":0,
"age":0
}
If you look at the data field I need a way of changing the data on that field to yyyy-mm-dd.
In other words .. if we take this example as a reference I would need the data field value:
This:
2017-09-14T23:00:00.000Z
To be changed to this:
2017-09-14
How can I do this?
You should just be able to slice away the trailing characters.
data.date = data.date.slice(0, 10);
This works because your data format is in a predictable form where the month and day have a 0 padding.
I recommend a library called momentjs for all your javascript date manipulation needs.
https://momentjs.com/
With it, you can do what you want easily: moment(data.date).format('YYYY-MM-DD')
Note that your JSON is invalid because the date string is not properly quoted, but assuming it's corrected and that your JSON object is named myObject, the following would work:
var date = new Date(myObject.date); //your string will become a date object
//now operate on that date object by extracting the year, month,
//and day into a string
console.log(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
Well you could convert the string to date and then format it
Here is an example
function formate(date) {
if (typeof date == "string")
date = new Date(date);
var day = (date.getDate() <= 9 ? "0" + date.getDate() :
date.getDate());
var month = (date.getMonth() + 1 <= 9 ? "0" +
(date.getMonth() + 1) : (date.getMonth() + 1));
var dateString = day + "-" + month + "-" +
date.getFullYear() + " " + date.getHours() + ":" +
date.getMinutes();
return dateString;
}

Rounding Date to nearest Day in Javascript.

I'm storing data on a per-day basis in localStorage, and in doing so I want to use the date as the "primary key".
I'm using JSON.stringify() and .parse() to store data thus:
localStorage.setItem(datakey, JSON.stringify(dataObject));
dataObject = JSON.parse(localStorage.getItem(datakey));
I want to use the date as the datakey, and the app will just overwrite data recorded earlier in the the day if you record again later in the day.
So I need to round the date to the current day, month and year.
At the moment I'm trying this:
selected_d = $("#date-1").val();
console.log("The date is "+selected_d);
dateArray = selected_d.split("-");
day = dateArray[2];
month = dateArray[1];
year = dateArray[0];
datakey = new Date(year, month, day);
console.log("The datakey is "+datakey);
The reason for using split is that the #date-1 is a jQuery Mobile date and it comes in a yyyy-mm-dd format and I want to use standard UK dd/mm/yy format.
The out put of the the console logs is:
The date is 2014-02-18
The datakey is Tue Mar 18 2014 00:00:00 GMT+0000 (GMT Standard Time)
I know this is because Jan = 0, Feb = 1 and so on.
What I'd really like is some way of creating an "ideal" date object for me. One that only holds days, months and years and one which is in the format DD/MM/YYYY so I can easily query the localStorage. I know I can reconstruct the date by doing:
var displayed_d = (day<10 ? '0' : '') + day + "/"+ (month<10 ? '0' : '') + month_up + "/" + current_d.getFullYear();
but it's not really ideal, is it?
Any ideas?
Why not just form the key using the API?
var d = new Date(); // or wherever the date comes from
var key = function(d) {
function two(n) {
return (n < 10 ? '0' : '') + n;
}
return two(d.getDate()) + '/' + two(d.getMonth() + 1) + '/' + d.getFullYear();
}(d);
You could add that as a function on the Date prototype:
Date.prototype.getDateKey = function() {
function two(n) {
return (n < 10 ? '0' : '') + n;
}
return two(this.getDate()) + '/' + two(this.getMonth() + 1) + '/' + this.getFullYear();
};
Now you can get a key easily:
var dateKey = someRandomDate.getDateKey();
MDN documentation for Date objects.

How to format a dateTime

Okay I have the following problem. I want to get the current dateTime and then want do check if a date that I enter is bigger than the current DateTime. The format of my dateTime should look like this.
03/11/2012 09:37 AM
Here is the function how I get the current DateTime.
function getCurrentDateTime()
{
var currentTime = new Date()
// Date
var month = currentTime.getMonth() + 1;
if (month < 10){
month = "0" + month;
}
var day = currentTime.getDate();
var year = currentTime.getFullYear();
// Time
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){
minutes = "0" + minutes;
}
if(hours > 11){
var dateString = month + "/" + day + "/" + year + " " + hours + ":" + minutes + " " + "PM";
test = new Date(dateString);
return dateString ;
} else {
var dateString = month + "/" + day + "/" + year + " " + hours + ":" + minutes + " " + "AM";
return dateString;
}
}
As you can see how it gives back a string. But when I want to covert it to a date with this function. I get this format Fri May 11 2012 09:37:00 GMT+0200 (Romance Daylight Time)
date = new Date(dateString);
And with this I can't calculate.
Could anybody help me how I can get the current date in this format so that I can do the check?
Kind regards.
Javascript provides very limited functionality for working with dates out of the box. Use an external library like momentjs.
For example, your function would be reduced to
var stringDate = moment().format("DD/MM/YYYY HH:mm A");
And you could convert that and compare it to the current time with
var earlierDate = moment(stringDate, "DD/MM/YYYY HH:mm A");
if (earlierDate.valueOf() < moment().valueOf()) {
// earlier indeed
}
datejs is another lib for solving date-manipulation problems

Categories

Resources