This question already has answers here:
milliseconds to time in javascript
(19 answers)
Closed 4 years ago.
How to convert time from milliseconds to date time in javascript.
I need to convert from milliseconds to below mentioned format.
format : 20-10-1994 06:00:00
You can use it that way.
var d = new Date(milliseconds);
var myDate = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
Reference:
https://www.w3schools.com/jsref/jsref_obj_date.asp
https://www.w3schools.com/js/js_date_methods.asp
Related
The purpose of this function is to return the current date. Why would this function return a date that is 15 days ago. Tested in FF and Chrome.
function whatTime() {
var currentdate = new Date();
var datetime = (currentdate.getMonth()+1) + "/" + currentdate.getDay() + "/" + currentdate.getFullYear() + " " + currentdate.getHours() + ":" + (currentdate.getMinutes()<10?'0':'') + currentdate.getMinutes() + ":" + (currentdate.getSeconds()<10?'0':'') + currentdate.getSeconds();
return datetime.toLocaleString();
}
Date.getDay() returns the day of the week, not the day of the month.
MDN documentation: Date.getDay()
Use Date.getDate() instead.
MDN documentation: Date.getDate()
I believe getDay() returns the day of the week, not the actual day.
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
I have a value that shows as Sat Apr 28 2018 00:00:00 GMT-0600 (MDT) but I want to convert it to just 4/28/2018. and when I do value.toISOString(); it gives me back 2018-04-26T06:00:00.000Z, how do i take off the end part?
You can do something like this
var d = new Date(); // Your date object
console.log(dateString = getDateString(d));
function getDateString(date) {
return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getYear();
// Or
return (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getYear();
}
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Convert date to another timezone in JavaScript
(34 answers)
Closed 5 years ago.
Input:- 2018-01-19T17:04:54.923Z;
Output:- 2018-01-19 17:04:54;
How can I get universal logic which will work in all browsers in JAVASCRIPT
You can use this:
var dt = new Date('2018-01-19T17:04:54.923Z');
var formatedString = dt.getFullYear() + "-" + dt.getMonth() + 1 + "-" + dt.getDate() + " " + dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
console.log(formatedString);
In javascript you can convert UTC date to local date format using below snippets.
// Your input date
var utcDate = new Date('2018-01-19T17:04:54.923Z');
//Converted UTC to local(IST, etc.,,)
console.log(utcDate.toUTCString());
/*
Output is : "Fri, 19 Jan 2018 17:04:54 GMT"
*/
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
$a = date('Y-m-d h:i:s');
echo $a;
result:
2017-08-18 09:14:02
Is there a way to get the date in the same format using javascript / jquery ?
Here's an approach that is easy to understand for newer JavaScript developers who come from an OOP background.
var date = new Date();
alert(date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + " " + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2) + ":" + ("0" + date.getSeconds()).slice(-2));
date.getMonth() + 1 because the months are 0 indexed.
EDIT: The above solution now adds the leading zeros to getMonth() and getDay(). The slice(-2) call is a common way to getting the last two characters from the string.
For example, if date.getMonth() returns a 9. I would get 09, and slice(-2) would return me the same 09.
But if date.getMonth() returns a 10. I would get 010, and slice(-2) would return the last two characters again. So, 10.
The other answers are correct, this one is just easier to understand from a beginners perspective.
Try this
var date = new Date();
var iso = date.toISOString().match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/)
var correctdate = iso[1] + ' ' + iso[2];
var DateWithTime = new Date();
console.log(DateWithTime); //Date 2017-08-18T17:29:34.660Z
var onlyDate = DateWithTime.toISOString().substring(0, 10);
console.log(onlyDate); //2017-08-18
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 8 years ago.
I'm having a hard time trying to figure this out
$datenow = date('Y-m-j H:i:s'); // 2014-08-19 17:56:13
I would like to generate the exact date format with JS, how I could do this?
This is how it should appears: 2014-08-19 19:57:59
I would go like this :)
date = new Date();
dateFormated = date.getFullYear() + '-' + (date.getMonth()+1) + '-' +
date.getDay() + ' ' + date.getHours() + ':' + date.getMinutes() + ':'
+ date.getSeconds();
http://jsfiddle.net/r5mdggc8/2/
There are no leading zeros, but you can add them by using condition on each "date item" like so:
dateItem = getDay();
if (dateItem.toString().length < 2) {
dateItem = '0' + dateItem;
}
Of course, you can make a function out of it.