Angularjs getcurrent time [duplicate] - javascript

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 3 years ago.
I am using the below code to get the current time in angularJS:
$scope.getDatetime = function() {
return (new Date()) + "abc.txt" ;
};
What is the correct code to the current time in YYYY-MM-DD-Hours-Minutes-Seconds?

Maybe you need this.
$scope.getDatetime = function() {
var date = new Date();
var day = date.getDate();
var month = ((date.getMonth() + 1) > 9) ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
return year+"/"+month+"/"+day+" "+hours+":"+minutes+":"+seconds;
};

var s = new Date().toISOString();
console.log(s);
s = s.slice(0,19);
s = s.replace('T','-');
s = s.replace(':','-');
s = s.replace(':','-');
console.log(s);

Related

JavaScript - Trying to set tomorrow's date using the code below [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
var today = new Date();
var tomorrow = today.setDate(today.getDate() + 1)
console.log(tomorrow)
1596607917318
I am getting 13 digit number after using setDate(). How can I get the date in 2 digit format?
Date outputs in JS often need some manual processing to be exactly what you want. Try this:
// Create new Date instance
var today = new Date();
var tomorrow = today;
// Add a day
tomorrow.setDate(tomorrow.getDate() + 1)
console.log(formatDateToString(tomorrow));
function formatDateToString(date) {
var dd = (date.getDate() < 10 ? '0' : '')
+ date.getDate();
var MM = ((date.getMonth() + 1) < 10 ? '0' : '')
+ (date.getMonth() + 1);
return dd + "/" + MM;
}
The Date object has different methods that you can use to get certain parts of the timestamp.
// for day-month (i.e.: Oct 31 is 31-10
let formatted = `${tomorrow.getDate()}-${tomorrow.getMonth() + 1}`
See more: https://www.w3schools.com/js/js_date_formats.asp
setDate has changed the date of today.
Therefore output today and don't assign what's returned by setDate.
var today = new Date();
today.setDate(today.getDate() + 1);
console.log(today.toLocaleDateString());
Month is zero based so getMonth() + 1 returns this month, getDate() + 1 returns tomorrow.
var fecha = new Date();
var year = fecha.getFullYear();
var mes = fecha.getMonth() + 1;
var dia = fecha.getDate() + 1;
var hora = fecha.getHours();
var minutos = fecha.getMinutes();
var segundos = fecha.getSeconds();
var output = `Date: ${dia}/${mes}/${year}`+ '\n' + `Time: ${hora}:${minutos}:${segundos}`;
console.log(output)
Nice question, I recently had to do something similar in VB. Here is a simple javascript version, based on your code:
//this gets the date today
var today = new Date();
//we add one, to get the date tomorrow
var tomorrow = today.getDate() + 1
//if tomorrow is a single digit number, we just pad it with a zero
if (tomorrow < 10)
{
tomorrow = '0' + tomorrow
}
//write to the console
console.log(tomorrow)

Javascript time, shows 2020.04.03 and time, How I can split year to show 20.04.03 [duplicate]

This question already has answers here:
How to get 2 digit year w/ Javascript? [duplicate]
(5 answers)
Closed 2 years ago.
Caould anbody please help me, how I can change my javascript code to show year 20 not 2020. I am trying to add this code
d = Date.now();
d = new Date(d);
d = d.getDate()+'.'+(d.getMonth()+1)+'.'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()
console.log(d);
But I cannot it get it write, how i should add this code to my code to get it right,. Thank you in advice
<script>
const pad = num => ("0" + num).slice(-2);
const timedate = () => {
const currentTime = new Date(new Date().getTime() + diff);
let hours = currentTime.getHours();
const minutes = pad(currentTime.getMinutes());
const seconds = pad(currentTime.getSeconds());
const d = currentTime.getDate();
console.log(d);
const day = pad(d);
const month = pad(currentTime.getMonth() + 1);
const yyyy = currentTime.getFullYear();
/* let dn = "PM"
if (hours <= 12) dn = "AM";
if (hours >= 12) hours -= 12;
if (hours == 0) hours = 12; */
hours = pad(hours);
timeOutput.value = "" +
yyyy + "." + month + "." + day +
" " +
hours + ":" +
minutes + ":" +
seconds// + dn;
}
let timeOutput;
let serverTime;
let diff;
window.addEventListener("load", function() {
timeOutput = document.getElementById("timedate");
serverTime = new Date;// change to new Date("[[:Date:]]"); for example
diff = new Date().getTime() - serverTime.getTime();
setInterval(timedate, 1000);
});
</script>
new Intl.DateTimeFormat("en", {year: "2-digit"}).format(new Date())

JavaScript struggling with date format [duplicate]

This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 5 years ago.
I am trying to retrieve a date but I am struggling with the month because January = 0 instead of 1. I need to retrieve items which occured after today -60 days.
var today = new Date();
if (today.getMonth() < 1) {
var numberOfDaysLastMonth = getDaysInMonth(12,today.getFullYear()-1); //number of days in december last year
var numberOfDaysThisMonth = getDaysInMonth(today.getMonth()+1,today.getFullYear()); //number of days this month
} else {
var numberOfDaysLastMonth = getDaysInMonth(today.getMonth(), today.getFullYear()); //number of days last month
var numberOfDaysThisMonth = getDaysInMonth(today.getMonth()+1, today.getFullYear()); //number of days this month
};
var startDate = new Date();
var myMonth;
var myYear;
if (today.getMonth() < 1) {
myMonth = today.getMonth() + 1;
myYear = today.getFullYear()-1;
} else {
myMonth = startDate.getMonth()-1;
myYear = today.getFullYear();
};
startDate = myYear+"-"+myMonth+"-"+startDate.getDate(); // returns last month
Is there a more simple (and working) way to do this?
If it's difficult to use dates as they are in Javascript, then modify them to work as they want them to work. An example:
Date.prototype.getMyMonth = function() {return this.getMonth() + 1;};
Now, you can change your code to use .getMyMonth instead of .getMonth.

Adding Day to Date [duplicate]

This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 5 years ago.
I have an example below of code that getting the date and adding certain days.
But the result I got from log is like these 1507824000000.
var endDate = new Date('10/03/2017');
var numOfDays = 10;
console.log(endDate.setDate(endDate.getDate() + numOfDays ));
If you want something to see your 10 days added, you can try the following :
var endDate = new Date('10/03/2017');
var numOfDays = 10;
endDate.setDate(endDate.getDate() + numOfDays);
var dd = endDate.getDate();
var mm = endDate.getMonth() + 1;
var y = endDate.getFullYear();
var yourNewDate = dd + '/'+ mm + '/'+ y;
console.log(yourNewDate)

Convert Military Hours to Normal Hours [duplicate]

This question already has answers here:
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 5 years ago.
I'd like my hours section to be set to 1–12 and not 0–23. Thank you.
Here's the JavaScript:
setInterval(function(){ time();}, 1000)
function time(){
var dates = new Date();
var newDates = dates.toDateString();
// var clock = dates.toLocaleTimeString();
var seconds = dates.getSeconds();
var minutes = dates.getMinutes();
var hours = dates.getHours();
var stringSeconds= String(seconds);
var stringMinutes= String(minutes);
var stringHours= String(hours);
newDate.textContent = newDates;
newDivSeconds.textContent = stringSeconds;
newDivMinutes.textContent = stringMinutes + ' :' ;
newDivHours.textContent = stringHours + ' :';
const usHours = (date.getHours() % 12) || 12;
Use the modulus operator
var usHours = date.getHours() % 12;

Categories