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.
Related
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.
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.
I have a date format like this 15/name of the month/2016 Hr:MM:SS this is coming to me as a string. I want to add one hour to the input string and display it as Local time.
I am trying to substring this but the format is not consistent as the name of the month has many characters in "November" and with "July".
Can any one please suggest how to to with this.. I am looking at regular expressions but i am not sure if reg helps...
The following will allow you to add one hour and convert to local time.
var dateString = "15/January/2016 23:59:59";
// Replace slashes
var dateStringReplaced = dateString.replace('/', ' ');
// Parse date
var date = new Date(Date.parse(dateStringReplaced));
// Add an hour
date.setHours(date.getHours() + 1);
console.log(date.toLocaleString());
https://jsbin.com/tecosovaki/1/edit?js,console
function UTCTime() {
var time = "MM/DD/YYYY HH:MM:SS AM\PM";
var months = ["Jan", 'Feb', "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var array = dtc_time.split(/[\/\s:]/);
var d = new Date(array[2], array[0] - 1, array[1], array[3], array[4], array[5], 0);
d.setHours(array[3] - 1);
var time = formatAMPM(d);
var local = d.getDate() + "/" + months[d.getMonth()] + "/" + d.getFullYear() + " " + time;
document.getElementById("demo").innerHTML = local.fontcolor("white");
}
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return strTime;
}
This is quite similar to Stack Overflow question Format a new Date() to EEE MMM dd HH:mm:ss zzz yyyy.
But here I need a date format something like:
"Fri, 23 May 2014, 09:03:29"
Any Suggestions?
Try this example. I hope it works!
var timeString = myDate.getFullYear() + '-' + (myDate.getMonth()+1) + '-'
+ myDate.getDate() + ' ' + myDate.getHours() + ':' + myDate.getMinutes() + ':'
+ myDate.getSeconds();
var d = new Date();
alert(d.toString().split("GMT")[0]);
Just split the GMT from your date object string
A function like this can solve the problem:
function formatDate(myDate) {
var abbrMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var abbrDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
function zeroPadding(val) {
return val.toString().length === 1 ? "0" + val : val;
}
return abbrDays[myDate.getDay()] + ", " + myDate.getDate() + " " + (abbrMonths[myDate.getMonth()]) +
" " + myDate.getFullYear() + ", " + zeroPadding(myDate.getHours()) + ":" +
zeroPadding(myDate.getMinutes()) + ":" + zeroPadding(myDate.getSeconds());
}
To get the formatted date just pass your date into the function like so:
var formattedDate = formatDate(new Date());
Moment is a plugin made especially for formatting and calculating dates. Perhaps it can also help you if you're looking on formatting dates in multiple ways.
When I generate a date with the following code
var date = new Date();
I get a date-time of the following form
Sat May 11 2013 21:54:23 GMT-0700 (PDT)
Can anyone tell me how to generate a date of the form below without using regex/string functions.
Sat May 11 2013 21:54:23
Try giving this a shot: http://blog.stevenlevithan.com/archives/date-time-format
It has a collection of Date format options that I believe would give you what you desire.
Ignoring the above Javascript library and doing it native:
var date = new Date();
date = date.toDateString() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
Does this work ok for you?
Javascript does not come with a date formatting library beyond the one specific format you see. You can either build your own by piecing together the exact pieces you want and adding the strings together or you can get a third party date formatting library.
If you want a 3rd party library, the Datejs library is pretty thorough. In that library, it would be:
Date.today().toString("ddd MMM d yyyy H:mm:ss");
You could, of course obtain all the component values from the built-in date object and then build your own string too.
Without adding a library, you'd have to write your own way to make that specific format:
function formatDate(date) {
function makeTwoDigits(val) {
var prefix = val <= 9 ? "0" : "";
return prefix + val;
}
var dayOfWeek = date.getDay(); // 0-6, 0=Sunday
var month = date.getMonth(); // 0-11
var day = date.getDate(); // 1-31
var year = date.getFullYear(); // 2013
var hours = makeTwoDigits(date.getHours()); // 0-23
var mins = makeTwoDigits(date.getMinutes()); // 0-59
var secs = makeTwoDigits(date.getSeconds()); // 0-59
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return days[dayOfWeek] + " " + months[month] + " " + day + " " + year + " " +
hours + ":" + mins + ":" + secs;
}
Working demo: http://jsfiddle.net/jfriend00/zu7Uz/