How to convert 1304921325178.3193 to yyyy-mm-dd -> in javascript?
I use highchart and I would like to convert data(xAxis[0]0) to yyyy-mm-dd.
I tried to parse the millisecond using this function
function(valTime) {
var date = new Date(valTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
m = (m < 10) ? '0' + m : m;
d = (d < 10) ? '0' + d : d;
return [y, m, d].join('-');
}
However, there is a gap between actual date(2015-01-26) and selected date in the chart (2015-01-29).
captured image
I guess if I calculate .3193, the date will be matched.
Is there any way to get the right date from the millisecond?
Your ms are actually pointing to 2011-05-09T06:08:45.178Z:
var date = new Date(1304921325178.3193); // Date 2011-05-09T06:08:45.178Z
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
console.log(`${year}-${month}-${day}`); // 2011-05-09
Related
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 months ago.
I want to create a function that converts all date formats into single date format (yyyy-mm-dd).
function taskDate(dt){
console.log(dt)
}
var dt = new Date("15-10-2022")
var d = dt.getDate();
var m = dt.getMonth() + 1;
var y = dt.getFullYear();
var dateString = y + '-' + (m <= 9 ? '0' + m : m) + "-" + (d <= 9 ? '0' + d : d);
taskDate(dateString)
the problem occurred in dd-mm-yyyy format its converts day into month automatically and when the day is more than 12 its returns, Nan.
To convert a date to the yyyy-MM-dd format, the Swedish locale can be used.
By the way, your date is actually invalid. Refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
var dt = new Date(2022, 9, 15); // the 2nd arg is monthIndex (starts from 0)
var str = dt.toLocaleString('sv-SE'); // Swedish locale
console.log(str);
You input date string is invalid to Date.
var dt = new Date('15-10-2022');
console.log(dt)
You can swap day of month and month and it will work as expected as:
var dt = new Date('10-15-2022');
console.log(dt);
FINAL CODE
function taskDate(dt) {
console.log(dt);
}
var dt = new Date('10-15-2022');
var d = dt.getDate();
var m = dt.getMonth() + 1;
var y = dt.getFullYear();
var dateString =
y + '-' + (m <= 9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
taskDate(dateString);
To convert from dd-mm-yyyy to mm-dd-yyyy as:
const date = '15-10-2022';
const [dd, mm, yy] = date.split('-');
const newDate = [mm, dd, yy].join('-');
console.log(newDate);
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
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();
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I am using pikaday's datepicker. It gives me output "Fri Sep 20 2013". How can I convert this date into yyyy-mm-dd format and I also would want following date of this selected date and set that one to another element.
I tried this code
function formattedDate() {
var fromdate = new Date(document.getElementById('datepicker').value);
var dd = fromdate.getDate();
var mm = fromdate.getMonth()+1; //January is 0!
var yyyy = fromdate.getFullYear();
if(dd < 10)
{
dd = '0'+ dd;
}
if(mm < 10)
{
mm = '0' + mm;
}
var fromdate1 = dd+'/'+mm+'/'+yyyy;
fromdate.setDate(fromdate.getDate() + 2);
document.getElementById('datepicker').value = fromdate1;
var newdate = fromdate;
document.getElementById('datepicker1').value = newdate;
//alert(newdate1);
}
But it doesn't work.
var date = new Date(dateString);
var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var properlyFormatted = "" + year + month + day;
Or
var date = new Date(dateString);
var properlyFormatted = date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2);
Use momentjs — it's a library available for using in browser and node projects
In your case you should use this pattern:
moment().format("YYYY-mm-D");
And you can try it in console on momentjs's site:
I am using the following to get the current date:-
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;
If I alert(newdate); it shows:-
3/06/2013
Is there any way I can display this as:-
03/06/2013
With plain Javascript, only manually
if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;
If you want to avoid using a library, and don't mind an extra line in your JavaScript:
var day = dateObj.getUTCDate(),
dd = parseInt(day, 10) < 10 ? '0' + day : day;
Using JQuery DateFormat:
$.format.date(dateObj.toString(), "dd/MM/yyyy");
var mydate=new Date();
alert(mydate.toString('dd/MM/yyyy'));
function padWithZeroes(number, width)
{
while (number.length < width)
number = '0' + number;
return number;
}
Now call day = padWithZeroes(day, 2) (and likewise for the month) before you use it.
You can split it and create your own format:
var splitTime = newDate.split("/");
var day = splitTime[0];
var month = splitTime[1];
var year = splitTime[2];
if (day < 10){
day = "0" + day;
}
var myDate = day + '/' + month + '/' + year;
Living demo:
http://jsfiddle.net/rtqpp/
Please use the following:
var dateObj = new Date();
var month = ('0' + (dateObj.getUTCMonth() + 1) ).slice( -2 );;
var day = ('0' + (dateObj.getUTCDate() + 1) ).slice( -2 );
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;