I'm getting this date format from back-end: 1970-01-01T10:59:00Z
How can I get the time from it with JavaScript and put it in this input:
<input type="time" ng-model='content.time' />
Thanks for your help!
You could use the getHours(), getMinutes() and getSeconds() function and concatenate the values.
var result = document.getElementById("result");
var dateTime = new Date();
var hours = new Date().getHours();
var minutes = new Date().getMinutes();
var seconds = new Date().getSeconds();
result.innerHTML = hours + ":" + minutes + ":" + seconds;
<p id="result"></p>
var date = new Date($scope.content.time);
var converted = date.getHours() + ":" + date.getMinutes();
console.log(converted);
This might help you:
var dt = new Date();
var tm = dt.getUTCHours();
For UTC.
Also you can use:
new dt.getHours()
new dt.getMinutes()
var date = new Date("1970-01-01T10:59:00Z");
var converted = date.toLocaleString();
console.log(converted);
Then you'd just assign the converted value to the model.
With angular, and not just vanilla javascript/DOM manipulation, in your controller js:
var dateTime=<referenceToDataRetrievedFromBackEnd>;
/*Format the date you've retrieved*/
var hours = new Date().getHours();
var minutes = new Date().getMinutes();
var seconds = new Date().getSeconds();
var formattedTimeString=hours + ":" + minutes + ":" + seconds
/*This is the part that actual ties the retrieved and reformatted data to the view*/
$scope.content.time=formattedTimeString;
In your controller add this function
var parseDateTime = function (input) {
vars = input.split('T');
date = vars[0].split('-');
time = vars[1].split(':');
//return date in 'yyyy-MM-dd H:i:s'
return date[0] + '-' + date[1] + '-' + date[2] + ' ' + time[0] + ':' + time[1] + ':00';
}
Then you can use it with your datetime
var dateTime = DATE_RECEIVED_FROM_BACKEND;
$scope.content.time = parseDateTime(dateTime);
Hope this helps :)
place this code in the controller where after the data u receive from backend
var d = new Date("1970-01-01T10:59:09Z");//pass the object which have ur date
$scope.content.time=d.getHours() + ":" + d.getMinutes()+":"+ d.getSeconds();
Related
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I have facing an issue in javascript date format all dates in this format yyyy-mm-dd HH:MM:ss
My Code:
var currentdate = new Date();
var prevdate = new Date();
var firstdate = new Date();
prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));
firstdate.setTime(currentdate.getTime() + (30 * 60 * 1000));
var current = currentdate.toLocaleTimeString();
var previous = prevdate.toLocaleTimeString();
var first = firstdate.toLocaleTimeString();
console data
console.log(previous); //10:28:24 PM
console.log(current); //10:58:24 PM
console.log(first); //11:28:24 PM
I try this , how can i pass previous and first date
var Currentdate=dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
console.log("Currentdate"); //2020-05-07 22:58:11
Expected Output Date Format: yyyy-mm-dd HH:MM:ss
previous date: 2020-05-07 22:28:11 // date before 30min
current date: 2020-05-07 22:58:11 // current date
first date: 2020-05-07 23:28:11 // date after 30min
What should i do? can anyone help?
You should use currentdate.toLocaleString() instead, as toLocaleTimeString()
returns a string with a language sensitive representation of the time portion of this date
toLocaleString
toLocaleTimeString
Use toLocaleString instead of toLocaleTimeString
Hi please try th following function:
function getTime(){
var date = new Date();
console.log(GetFormattedDate(date));
}
function GetFormattedDate(date) {
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + (date.getDate())).slice(-2);
var year = date.getFullYear();
var hour = ("0" + (date.getHours())).slice(-2);
var min = ("0" + (date.getMinutes())).slice(-2);
var seg = ("0" + (date.getSeconds())).slice(-2);
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg;
}
in your case
function getTime(){
var date = new Date();
var currentdate = new Date();
var prevdate = new Date();
var firstdate = new Date();
prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));
firstdate.setTime(currentdate.getTime() + (30 * 60 * 1000));
console.log(GetFormattedDate(prevdate));
console.log(GetFormattedDate(currentdate));
console.log(GetFormattedDate(firstdate));
}
function GetFormattedDate(date) {
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + (date.getDate())).slice(-2);
var year = date.getFullYear();
var hour = ("0" + (date.getHours())).slice(-2);
var min = ("0" + (date.getMinutes())).slice(-2);
var seg = ("0" + (date.getSeconds())).slice(-2);
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg;
}
regards
You need to use toLocaleString
var currentdate = new Date();
var prevdate = new Date();
var firstdate = new Date();
prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));
firstdate.setTime(currentdate.getTime() + (30 * 60 * 1000));
var options = { hour12: false };
var current = currentdate.toLocaleString('en-US', options);
var previous = prevdate.toLocaleString('en-US', options);
var first = firstdate.toLocaleString('en-US', options);
current = current.replace(/\//g, '-');
previous = previous.replace(/\//g, '-');
first = first.replace(/\//g, '-');
console.log(`current: ${current}`);
console.log(`previous: ${previous}`);
console.log(`first: ${first}`);
My time format is given below
var time = "2013-08-07 20:53:35"
I need to get the date "2013-08-07" as 07.08.2013 and time "20:53:35" as 20:53:35.
To achieve that you should
first separate date and time using split(" ")
Then split() date by "-"
Use reverse() on the date array and join it using .
var time = "2013-08-07 20:53:35"
let arr = time.split(' ');
let date = arr[0].split('-').reverse().join('-');
let tm = arr[1]
console.log(date)
console.log(tm)
depending on how much of this you will be doing I would highly recommend look at moment.js. You can download from npm if a nodejs app or just use from a cdn if browser based javascript. Here is a sample:
var time = "2013-08-07 20:53:35"
var newdate = moment(time).format("DD.MM.YYYY");
var newtime = moment(time).format("HH:mm:ss");
document.writeln(newdate);
document.writeln(newtime);
You can do like this using core javascript
function formatDate(datetime) {
var date = new Date(datetime);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var strTime = hours+':'+minutes+ ':' + seconds;
return ('0' + (date.getMonth()+1)).slice(-2) + "." +('0' + (date.getDate())).slice(-2) + "."+ date.getFullYear() + " " + strTime;
}
console.log(formatDate('2013-08-07 20:53:35'));
https://www.quackit.com/javascript/javascript_date_and_time_functions.cfm
http://jsfiddle.net/p2wLrov4/2/
I have a date string which coming from the db as follows
/Date(1469167371657)/
Is there any way to convert this date to following format using javascript
MM/DD/YYYY HH:MM
I've searched a lot but unble to find a solution
In plain javascript you have to write your own function for string format a date, for example for your string format:
var date = new Date(1469167371657);
function stringDate(date) {
var mm = date.getMonth()+1;
mm = (mm<10?"0"+mm:mm);
var dd = date.getDate();
dd = (dd<10?"0"+dd:dd);
var hh = date.getHours();
hh = (hh<10?"0"+hh:hh);
var min = date.getMinutes();
min = (min<10?"0"+min:min);
return mm+'/'+dd+'/'+date.getFullYear()+" "+hh+":"+min;
}
console.log(stringDate(date));
drier code version
var date = new Date(1469167371657);
function stringDate(date) {
return ("0" + (date.getMonth() + 1)).slice(-2)+'/'
+("0" + date.getDate()).slice(-2)+'/'
+date.getFullYear()+" "
+("0" + date.getHours()).slice(-2)+':'
+("0" + date.getMinutes()).slice(-2)
}
console.log(stringDate(date));
with pure js you can do the folowing
var d = new Date();
console.log(d.getMonth() + 1 + "/" + d.getDate() + "/" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes())
You can use - http://momentjs.com/ and have it done like:
moment(1469167371657).format('MM/DD/YYYY HH:MM')
You can do this with the following steps:
1) convert the timestamp to a date object.
var timestamp = "/Date(1469167371657)/"; // However you want to save whatever comes from your database
timestamp = timestamp.substr(timestamp.indexOf("(")+1); // gives 1469167371657)/
timestamp = timestamp.substr(0,timestamp.indexOf(")")); // gives 1469167371657
var d = new Date(timestamp);
2) set it to your format
function leadZero(i) {if(i < 10) {return "0"+i;} return i;} // Simple function to convert 5 to 05 e.g.
var time = leadZero(d.getMonth()+1)+"/"+leadZero(d.getDate())+"/"+d.getFullYear()+" "+leadZero(d.getHours())+":"+leadZero(d.getMinutes());
alert(time);
Note: the date / timestamp you provided is too high for javascript to understand, so this example will not work correclty
I believe that number is milliseconds so to convert it to date, you would do this:
var time = new Date().getTime();
var date = new Date(time);
alert(date.toString()); // Wed Jan 12 2011 12:42:46 GMT-0800 (PST)
var time=1469167371657;
var date = new Date(time);
alert(date.toString());
I am calculating the time until 11:59PM of the current day. Here is an example.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.js"></script>
<script>
setInterval(function() {
var now = moment();
var mid = moment();
mid = mid.endOf('day');
var diffHours = mid.diff(now, 'hours');
var diffMinutes = mid.diff(now, 'minutes');
var diffSeconds = mid.diff(now, 'seconds');
console.log(diffHours + "h " + diffMinutes + "m " + diffSeconds + "s");
}, 1000)
</script>
However, I was hoping it would show me a time such as 20h 13m 49s, instead I am getting 20h 1255m 73500s
I understand this is working as intended pretty much, but how can I achieve the format I am seeking?
You'll want to modify your now variable after each diff.
var hours = mid.diff(now, 'hours'); //Get hours 'till end of day
now.hours(now.hours() + hours);
var minutes = mid.diff(now, 'minutes');
Just for comparison, here's David Stampher's moment.js example from a comment on another answer giving the time until 23:59:59.999 "today" and the same functionality in plain JS:
// Moment.js version
var now = moment();
var mid = moment();
mid = mid.endOf('day');
var diff1 = moment.utc(moment(mid, "DD/MM/YYYY HH:mm:ss")
.diff(moment(now, "DD/MM/YYYY HH:mm:ss")))
.format("HH:mm:ss");
console.log('moment.js: ' + diff1);
// POJS version
var z = (n) => (n<10? '0' : '') + n;
var ms = new Date().setHours(23,59,59,999) - new Date();
var diff2 = z(ms/3.6e6|0) + ':' +
z(ms%3.6e6/6e4|0) + ':' +
z(ms%6e4/1e3|0);
console.log('plain js : ' + diff2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
I'm using this javascript code to sync the client time with my server time
var offset = 0;
function calcOffset() {
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("GET", "http://stackoverflow.com/", false);
xmlhttp.send();
var dateStr = xmlhttp.getResponseHeader('Date');
var serverTimeMillisGMT = Date.parse(new Date(Date.parse(dateStr)).toUTCString());
var localMillisUTC = Date.parse(new Date().toUTCString());
offset = serverTimeMillisGMT - localMillisUTC;
}
function getServerTime() {
var date = new Date();
date.setTime(date.getTime() + offset);
return date;
}
the date I get back is
"2013-10-03T16:37:05.568Z"
How to I make this "2013-10-03 H:i:s"?
Although using moment.js is a smoother way to do it if you are working with a bunch of dates, here's a way to do it with vanilla JS:
x = new Date
x.getFullYear() + '-' + x.getMonth() + '-' + x.getDay()
Edit:
Here it is with the time and leading zeros on the month and day, as you can see these extra things add a good bit more code. Maybe if you post another question detailing your troubles with moment.js, we would be able to help getting it fixed:
formatDate(new Date);
function formatDate(d){
var year = d.getFullYear();
var month = addLeadingZero(d.getMonth());
var day = addLeadingZero(d.getDay());
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}
function addLeadingZero(n){ return n < 10 ? '0'+n : ''+n }