I am getting the date format in JSON respose like below
14-JAN-14
I need to convert it in my ajax success function like below
2014-01-14
How can I do this? Thanks
Here's how to do it in JS:
var d = new Date('14-Jan-14');
var curr_date = d.getDate();
var curr_month = ("0" + (d.getMonth() + 1)).slice(-2);
var curr_year = d.getFullYear();
document.write(curr_year + "-" + curr_month + "-" + curr_date);
I've added in the add a zero and slice the last two characters so that you have your double digit date month format too.
Here's the fiddle.
Related
I have a string that looks like '1/11/2018 12:00:00 AM' and I want to reformat it to dd-mm-yyyy.
Keep in mind that the month can be double digit sometimes.
You can use libraries like moment.js. Assuming either you do not want to use any external library or can not use it, then you can use following custom method:
function formatDate(dateStr) {
let date = new Date(dateStr);
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
return day + '-' + month + '-' + year;
}
console.log(formatDate('1/11/2018 12:00:00 AM'));
You can do somethink like this :
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
console.log(curr_date + "-" + curr_month + "-" + curr_year);
However best way is with Moment.js,where you can Parse, Validate, Manipulate, and Display dates in JavaScript.
example:
var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');
function convertDate(oldDate) {
var myDate = new Date(Date.parse(oldDate)); //String -> Timestamp -> Date object
var day = myDate.getDate(); //get day
var month = myDate.getMonth() + 1; //get month
var year = myDate.getFullYear(); //get Year (4 digits)
return pad(day,2) + "-" + pad(month, 2) + "-" + year; //pad is a function for adding leading zeros
}
function pad(num, size) { //function for adding leading zeros
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
convertDate("1/11/2018 12:00:00 AM"); //11-01-2018
Demo here
I have read a lot of thread about the subject, but i'm not much into programming... i've made some tests but never acheive what i wanted.
Here we have a html template we have to modify to suit our needs.
here the code i have to edit:
<p id="DATE"></p>
<script>
var d = new Date();
document.getElementById("DATE").innerHTML = d.toString();
</script>
Its giving me the date OK...but not i the format i want...
the format i want is... DD-MM-YYYY HH:MM
Simple has that :)
Its been a couple of hours and i can't do it... Can anyone help with a code i just can copy/paste in my html file.
As opposed to using an external JavaScript library for a simple task. You should be able to achieve the same with Vanilla js.
Refer to this:
Where can I find documentation on formatting a date in JavaScript?
In short:
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
console.log(curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min);
The above snippet should have all the code you need.
For your specific implementation, you will need to do the below:
<script>
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var formattedDate = curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min;
document.getElementById("DATE").innerHTML = formattedDate;
</script>
A function for the format you require is fairly simple. The following will return a string for a provided Date, or the current date if none passed.
function formatDate(date) {
var d = date || new Date();
function z(n){return (n<10?'0':'')+n}
return z(d.getDate()) + '-' +
z(d.getMonth()+1) + '-' +
d.getFullYear() + ' ' +
z(d.getHours()) + ':' +
z(d.getMinutes());
}
document.write(formatDate());
BTW, you can't use the same token for two separate parts. While there is no standard or even consistency for formatting tokens, your format would be more commonly expressed as:
DD-MM-YYYY HH:mm
Which is compatible with moment.js and a few others, though there are many other token sets to represent the same format.
To achieve your goal simply try following codes:
First of all, inside the head tag add the below line of code:
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
Finally, change your existing line of code:
document.getElementById("DATE").innerHTML = moment(d).format('DD-MM-YYYY HH:MM');
Happy coding :)
I have the following value:
javascript variable:
"2015-10-14T17:54:19.033"
I want to end up with
mm/dd/yyyy
e.g.
10/14/2015
I was trying to do
var date = month[d.getMonth()] + " " + d.getDay()+ ", " + d.getFullYear();
Here is my Fiddle
http://jsfiddle.net/bthorn/7ptcedt4/
var d = new Date('2015-10-14T17:54:19.033');
var date = d.getMonth().toString() + "/" + d.getDay().toString() + "/" + d.getFullYear().toString();
console.log(date);
Something is NOT right: I am getting
9/3/2015
You can use this:
var date = new Date("2015-10-14T17:54:19.033");
var formatedDateString = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Or use moment.js
var formatedDateString = moment("2015-10-14T17:54:19.033").format('MM/dd/YYYY');
Or may you consider create your own date format function.
PS.: javascript months are 0-indexed, so for the correct month you should use (date.getMonth() + 1)
I'm trying to convert a date in javascript from MM/dd/yyyy to yyyy/MM/dd
so this works:
var d = new Date("08/08/2012");
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
output = 2012/7/8
///////////////////////////////////////////////////
this does not:
var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
and neither does this:
var dateString = "08/08/2012";
var d = Date.parse(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
how do I make it work with a string variable? thanks
~Myy
var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
That should, and does, work. Keep in mind that JavaScript stores months as a zero-indexed value.
If you want to have leading zeros, then you'll have to do some magic:
var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + ('0' + (d.getMonth()+1)).slice(-2) + "/" + ('0' + d.getDate()).slice(-2);
document.write(dateString);
jsFiddle
The reason why your Date.parse( ) example is not working, is because that function returns a timestamp (number of milliseconds since 1970), instead of a Date object. Therefore, you can't call functions like getFullYear() on the timestamp.
If all you need to do is re-order the values, you can do:
var dateString = "08/08/2012";
var dateElements = dateString.split("/");
var outputDateString = dateElements[2] + "/" + dateElements[0] + "/" + dateElements[1];
document.write(outputDateString );
I can confirm with MrSlayer that the code works in jsFiddle.
Your attempts at using Date.parse() should actuall be using Date(String(dateString)).
Don't forget to add 1 for each month.
How to split the date,time using java script ? I am getting date,time 2010-05-04 20:13:12.0.But i need only date.
date format mm/dd/yyyy
var cdate = gridline[i=1]["CDATE"];
Now i need cdate as mm/dd/yyyy only.
Please help me.
function getDateFromString(datestring) {
var d = new Date(datestring);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
return curr_date + "-" + curr_month + "-" + curr_year;
}
Source: webdevelopersnotes.com