Convert DD-MM-YYYY to YYYY-MM-DD format using Javascript - javascript

I'm trying to convert date format (DD-MM-YYYY) to (YYYY-MM-DD).i use this javascript code.it's doesn't work.
function calbill()
{
var edate=document.getElementById("edate").value; //03-11-2014
var myDate = new Date(edate);
console.log(myDate);
var d = myDate.getDate();
var m = myDate.getMonth();
m += 1;
var y = myDate.getFullYear();
var newdate=(y+ "-" + m + "-" + d);
alert (""+newdate); //It's display "NaN-NaN-NaN"
}

This should do the magic
var date = "03-11-2014";
var newdate = date.split("-").reverse().join("-");

Don't use the Date constructor to parse strings, it's extremely unreliable. If you just want to reformat a DD-MM-YYYY string to YYYY-MM-DD then just do that:
function reformatDateString(s) {
var b = s.split(/\D/);
return b.reverse().join('-');
}
console.log(reformatDateString('25-12-2014')); // 2014-12-25

You can use the following to convert DD-MM-YYYY to YYYY-MM-DD format using JavaScript:
var date = "24/09/2018";
date = date.split("/").reverse().join("/");
var date2 = "24-09-2018";
date2 = date.split("-").reverse().join("-");
console.log(date); //print "2018/09/24"
console.log(date2); //print "2018-09-24"

You just need to use return newdate:
function calbill()
{
var edate=document.getElementById("edate").value;
var myDate = new Date(edate);
console.log(myDate);
var d = myDate.getDate();
var m = myDate.getMonth();
m += 1;
var y = myDate.getFullYear();
var newdate=(y+ "-" + m + "-" + d);
return newdate;
}
demo
But I would simply recommend you to use like #Ehsan answered for you.

First yo have to add a moment js cdn which is easily available at here
then follow this code
moment(moment('13-01-2020', 'DD-MM-YYYY')).format('YYYY-MM-DD');
// will return 2020-01-13

Related

How to change format to standard time JavaScript

I have a date string in JavaScript which is arranged in a particular manner. How do I rearrange this in order to fit standard time?
let date = "2020-06-01T00:00:00Z"
How do I rearrange the date variable in order to match the format MM/DD, YYYY ?
Change your code to as follows:
let date = "2020-06-01T00:00:00Z";
date = new Date(date);
var dd = date.getDate();
var mm = date.getMonth()+1;
var yyyy = date.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm};
console.log(mm+'/'+dd+', '+yyyy)
i don't real know this is possible but i found the function you can use instead
function GetFormattedDate() {
var todayTime = new Date();
var month = todayTime.getMonth() + 1;
var day = todayTime.getDate();
var year = todayTime.getFullYear();
return month + "/" + day + "/" + year;
}
console.log(GetFormattedDate());
so you can add more in the function or edit what the function will return as a format according to your will

Converting date format from mm/dd/yyyy to yyyy-mm-dd format after entered

In my datepicker the date will be inserted in mm/dd/yyyy format. But after I inserted I want it to be sent in yyyy-mm-dd format. I am using JavaScript to do this. But I wasn't able to do that. So what should I do?
Thanks & regards,
Chiranthaka
you could also use regular expressions:
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
The expression also works for single digit months and days.
I did the opposite for my website, but it might help you. I let you modify it in order to fit your requierements. Have fun !
getDate
getMonth
getFullYear
Have fun on W3Schools
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
if(curr_month < 10)
curr_month = "0"+curr_month;
if(curr_date < 10)
curr_date = "0"+curr_date;
var curr_date_format = curr_date+"/"+curr_month+"/"+curr_year;
Adding more to Christof R's solution (thanks! used it!) to allow for MM-DD-YYYY (- in addition to /) and even MM DD YYYY. Slight change in the regex.
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})[\/ -](\d{1,2})[\/ -](\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
As Christof R says: This also works for single digit day and month as well.
// format from M/D/YYYY to YYYYMMDD
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear();
var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
return "".concat(yyyy).concat(mm).concat(dd);
};
var siku = new Date();
document.getElementById("day").innerHTML = siku.yyyymmdd();

Convert UTC Date to dd/mm/yyyy Format

I am having some difficulties when trying to convert UTC Date format to dd/mm/yyyy in JavaScript:
var launchDate = attributes["launch_date"];
if (isBuffering) {
var date = new Date(launchDate);
var d = new Date(date.toLocaleDateString());
launchDate = ((d.getUTCMonth() + 1) + "/" + (d.getUTCDate() + 1) + "/" + (d.getUTCFullYear()));
}
I tried with this, but it returns me an invalid date. So I changed to this:
var launchDate = attributes["launch_date"];
if (isBuffering) {
var date = new Date(launchDate);
var d = formatDate(new Date(date.toLocaleDateString()));
launchDate = ((d.getUTCMonth() + 1) + "/" + (d.getUTCDate() + 1) + "/" + (d.getUTCFullYear()));
}
However, it still returning me invalid Date. I wonder is there any possible way to change the date format of Fri May 31 2013 17:41:01 GMT+0200 (CEST) to dd/mm/yyyy?
Thanks in advance.
var d = new Date();
var n = d.toLocaleDateString();
This will be more superior in build JS method!
function formatDate(d)
{
date = new Date(d)
var dd = date.getDate();
var mm = date.getMonth()+1;
var yyyy = date.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm};
return d = dd+'/'+mm+'/'+yyyy
}
Try it:
Date.parseExact(Your_Date, 'dd/MM/yyyy').toString('MM/dd/yyyy');
or
Date.parseExact(Your_Date, 'MM/dd/yyyy').toString('dd/MM/yyyy');
Month is 0 indexed, but day is not. You don't need to add 1 to your day.
Also, you're formatting it for MM/dd/yyyy, not dd/MM/yyyy.
solution:
var launchDate = attributes["launch_date"];
if (isBuffering) {
var date = new Date(launchDate);
var d = formatDate(new Date(date.toLocaleDateString()));
launchDate = ((d.getUTCDate())+ "/" + (d.getUTCMonth() + 1) + "/" + (d.getUTCFullYear()));
}

Convert date in javascript

How can I convert this date:
2011-11-02T10:41:43+0000
Into this using JavaScript:
02/11
Thankful for all help!
If that date is a string, a simple RegEx can offer the desired results:
var date = "2011-11-02T10:41:43+0000";
date = date.match(/-(\d{1,2})-(\d{1,2})T/);
date = date[2] + "/" + date[1]; // date = "02/11"
var d = new Date('2011-11-02T10:41:43+0000'),
dateString = d.getDate()+'/'+(d.getMonth()+1);
console.log(dateString); // 2/11
You could do this
var d = new Date('2011-11-02T10:41:43+0000');
var e = d.getUTCDate() + '/' + (d.getUTCMonth() + 1);
alert(e);
Example: http://jsfiddle.net/pRbZ2/
Assuming that the 11 you want is the month and not the year

How to convert Regular Date to unixtime in Javascript?

How can i convert date which looks like this 12092008 to unixtime like this 1221215809
You may want to use the following:
function covertToUnixTime(yourDate) {
return new Date(yourDate.substring(4, 8) + '/' +
yourDate.substring(2, 4) + '/' +
yourDate.substring(0, 2)).getTime() / 1000;
}
covertToUnixTime('12092008'); // Returns: 1221170400
You could use jQuery datepicker's utility functions, parseDate and formatDate.
This worked for me.
var day = $("#day").val();
var month = $("#month").val();
var year = $("#year").val();
var date = new Date();
date.setFullYear(year, month, day)
var unixtimeMS = date.getTime();
var unixtime = parseInt(unixtimeMS / 1000);
alert(unixtime);

Categories

Resources