Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
how do I code the format Thu, Mar 4, 2021, 2:25 PM for time and date using Javascript. I tried using
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
var nameDay = daylist[day];
var date = nameDay + "," + " " + (today.getMonth()+1) + " " + today.getDate() + "," + " " + today.getFullYear() + "," + " "
let d = new Date();
let daylist = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
let monthlist = ["Jan","Feb","March","Apr ","May","June","July", "Aug", "Sep", "Oct", "Nov", "Dec"];
let amOrPm = (d.getHours() < 12) ? "AM" : "PM";
let hour = (d.getHours() < 12) ? d.getHours() : d.getHours() - 12;
let date = daylist[d.getDay()]+','+ monthlist[d.getMonth()]+ " " + d.getDate()+','+' ' +d.getFullYear()+',' + ' ' + hour + ':' + d.getMinutes()+ ' ' + amOrPm;
Just use the date-fns package.
Manual date manipulation is not worth getting into.
Related
This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 3 years ago.
Trying to add a number new date in javascript
How ever the number is coming in from a json file.
Here is what i have.
myObj = {"yearsleft":"2", "name": "john"};
var term = myObj.yearsleft;
var d = new Date();
var year = d.getFullYear() + term.toString();
var month = d.getMonth()+1;
var day = d.getDate();
var output = ''+ (day<10 ? '0' : '') + day + '/' + (month<10 ? '0' : '') + month + '/' + year;
alert(output);
above is a working example
However its just appending 2 to the end of the year. which isnt what i want it to do.
I want it to add onto 2019
if that's possible.
You are adding string to a number, convert it to int and then add.
var year = d.getFullYear() + parseInt(term.toString(), 10);
Convert to a number - currently you're concatenating not adding.
myObj = {"yearsleft":"2", "name": "john"};
var term = myObj.yearsleft;
var d = new Date();
var year = d.getFullYear() + +term;
var month = d.getMonth()+1;
var day = d.getDate();
var output = ''+ (day<10 ? '0' : '') + day + '/' + (month<10 ? '0' : '') + month + '/' + year;
console.log(output);
d.getFullYear() + parseFloat(term);
Seem to have fixed it for me.
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 4 years ago.
I need to check whether a string is between NOW and 5 minutes ago
I've managed to get the current date + time and the 5 minutes ago, but I'm struggling on comparing this two dates.
What I have, is a class that prints a few dates and I'd need to find if one of those dates is within the past 5 minutes
HTML:
<span class="msl_info">You have responded 3 times: on 21 Sep 2018 at 10:49, 21 Sep 2018 at 10:40, 21 Sep 2018 at 10:15.</span>
JavaScript:
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var hour = d.getHours();
var minute = d.getMinutes();
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
//var x = document.getElementById("demo");
var hour = addZero(d.getHours());
var minute = addZero(d.getMinutes());
var minuteAgo = addZero(d.getMinutes() - 5);
//x.innerHTML = h + ":" + m;
//Today minus 5 minutes
var dateFrom = curr_date + " " + m_names[curr_month] + " " + curr_year + " at " + hour + ":" + minuteAgo;
//Now
var dateTo = curr_date + " " + m_names[curr_month] + " " + curr_year + " at " + hour + ":" + minute;
console.log(dateFrom); //21 Sep 2018 at 10:38
console.log(dateTo); // 21 Sep 2018 at 10:43
This is a fiddle
Quick example about how to solve your problem:
const e = document.getElementById('msl_info');
const dates = e.innerHTML.match(/(\d{2}\s\w{3}\s\d{4}\sat\s\d{2}:\d{2})/g);
const realDates = dates.map((date) => {
const regExp = /(\d{2}\s\w{3}\s\d{4})\sat\s(\d{2}:\d{2})/;
const parsedDate = regExp.exec(date);
return Date.parse(parsedDate[1] + ' ' + parsedDate[2]);
});
function isDateInsideInterval(date) {
const now = new Date().getTime();
const past = new Date(now - (5 * 60 * 1000)).getTime();
return date >= past && date <= now ? true : false;
}
realDates.forEach((date) => {
console.log('Is in interval: ', isDateInsideInterval(date));
});
<span id="msl_info" class="msl_info">You have responded 3 times: on 21 Sep 2018 at 14:16, 21 Sep 2018 at 10:40, 21 Sep 2018 at 14:15.</span>
I read your span content looking for dates. I transforn the dates into Date and check the interval.
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I should format my datetime value in javascript to this format yyyy-MM-dd hh:mm:ss
I tried this
var btf = new Date(value.createDate);
var billingTimeFormatted = btf.getFullYear() + "-" + btf.getMonth() + "-" + btf.getDate() + " " + btf.getHours() + ":" + btf.getMinutes() + ":" + btf.getSeconds();
But it result to this
2017-8-31 02:00:00
month and date should be 2-digit (08 intead of 8)
What could be the best workaround?
*type on minutes is edited
Nothing wrong with your code. Javascript returns the integer < 10 in single digit only. Format it to string of 2 with a function.
function formatWithZero(val)
{
// as per comment by #RobG below, return ('0'+val).slice(-2) will also
// do the same thing in lesser lines of code. It works and can be used.
var value = val.toString();
if(value.length > 1)
return value;
var str = "00"+value;
return str.slice(str.length-2,str.length); ;
}
//I am using Date.now(), you can use your value.
var btf = new Date(Date.now());
var billingTimeFormatted = btf.getFullYear() + "-" + formatWithZero(btf.getMonth()) + "-" + formatWithZero(btf.getDate()) + " " + formatWithZero(btf.getHours()) + ":" + formatWithZero(btf.getMinutes()) + ":" + formatWithZero(btf.getSeconds());
alert(billingTimeFormatted);
You forgot () for getMinutes(), to have 2 digits :
var btf = new Date();
var currentMonth = btf.getMonth();
if (currentMonth < 10) { currentMonth = '0' + currentMonth; }
var billingTimeFormatted = btf.getFullYear() + "-" + currentMonth + "-" + btf.getDate() + " " + btf.getHours() + ":" + btf.getMinutes() + ":" + btf.getSeconds();
I used a plugin called jQRnageSlider and tried to get the date and time label showed in the slider, but when I scroll back the slider to last year, the month 12 turns out to be 11 eventually.
Normal case if I don't scroll back to last year.
It jumped to Nov 2016 immediately
That should be the issue of date formatting issue. Can anyone help?
$(".date-range-slider").dateRangeSlider({
...
formatter: function(val){
var days = ('0' + val.getDate()).slice(-2),
month = ('0' + val.getMonth() + 1).slice(-2),
year = val.getFullYear(),
hour = ('0' + val.getHours()).slice(-2),
min = ('0' + val.getMinutes()).slice(-2);
return days + "-" + month + "-" + year + " " + hour + ":" + min;
}
});
The problem is that line:
month = ('0' + val.getMonth() + 1).slice(-2)
You want to sum it mathematically not as string, so you should do:
month = ('0' + (val.getMonth() + 1)).slice(-2)
Check the snippet to see what your code is actually returning:
var val = new Date();
document.write('0' + val.getMonth() + 1)
And what returns corrected version:
var val = new Date();
document.write('0' + (val.getMonth() + 1))
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 6 years ago.
Want to get Date output in 2 different formats. Here is what i have at the moment.
<p id="demo"></p>
<script>
var d = new Date(day, month);
document.getElementById("demo").innerHTML = d.toString();
</script>
Does not work for me.
With this code i want to get this output: 21 Jun
Also would like to know how to get date in this format:
Jun 21, 2016 12:00 AM
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var h = d.getHours();
var ap = "AM";
if (h > 12) {
h-=12;
ap = "PM";
}
var dateString = months[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() + " " + h + ":" + d.getMinutes() + " " + ap;
var d = new Date();
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var h = d.getHours();
var ap = "AM";
if (h > 12) {
h-=12;
ap = "PM";
}
var dateString = months[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() + " " + h + ":" + d.getMinutes() + " " + ap;
document.getElementById("demo").innerHTML = dateString.toString();
Try This one.
JavaScript doesn't have functions to format the dates, so you'll have to do it manually. You can use this code for your first format:
<p id="demo"></p>
<script>
var d = new Date(2016, 5, 21);
document.getElementById("demo").innerHTML = getMonthName(d.getMonth()) + " " + d.getDate();
function getMonthName(month) {
var monthnames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return monthnames[month];
}
</script>
You can add the code to do any other format you want. However, you'll quickly realize the reason why we have many libraries for that purpose. So, don't reinvent the wheel and just use one of those libraries.