This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 1 year ago.
How do I can set the date format to "June 10, 2021"?
I have a small piece of code like this:
var homnay = new Date();
var datehomnay = (homnay.getMonth()+1) + ' ' + homnay.getDate() + ', ' + homnay.getFullYear();
document.getElementById("curDay").innerHTML = datehomnay;
Use this procedure
const MONTHS = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER']
const date = new Date()
const dateString = `${MONTHS[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`
console.log(dateString)
Related
This question already has answers here:
for( in ) loop index is string instead of integer [duplicate]
(5 answers)
Closed 2 years ago.
i'm new to JavaScript. Im trying to make a loop that prints every month and also numbers them, but i end up getting a concatenation of the numbers. here's my code
var months = ['january', 'february', 'march','april',
'may', 'june', 'july', 'august',
'september', 'october', 'november, 'december'];
for(i in months){
document.write((i+1)+'.- '+months[i]);
document.write('<br >')
}
and my output is like this:
01.- janury
11.- february
21.- march
.... etc
This code will give you the desired result, changing it to a standard for loop:
var months = ['january', 'february', 'march','april', 'may', 'june', 'july', 'august' 'september', 'october', 'november', 'december'];
for (i=0; i < months.length; i++) {
document.write(i+1 + ".- " + months[i] + "<br>")
};
This question already has answers here:
Get month name from Date
(40 answers)
Closed 2 years ago.
const getCurrentDate = () => {
var date = new Date().getDate();
var month = new Date().getMonth();
return date + "-" + month;
};
I want to transform the number on the month var to letters f.e " 2-8 to 2 - August "
Create a const-array for the months and get the month-th element of it.
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const getCurrentDate = () => {
var date = new Date().getDate();
var month = new Date().getMonth();
return date + "-" + MONTHS[month];
};
console.log(getCurrentDate());
This question already has answers here:
How to get current date in jQuery?
(32 answers)
Closed 6 years ago.
How can I get the current date in JQuery as a variable and then append to a heading in with an id of currentdate, with the format 24th July 2016?
In XClass library , just do :
new Date().format('ddS mmmm yyyy')
dd : day of month
S : suffix (st,nd,th,..)
mmmm: Full name of month
yyyy : year
DEMO
var out=document.getElementById('out');
out.innerHTML=new Date().format('ddS mmmm yyyy');
<script src="https://rawgit.com/abdennour/xclass/master/node_modules/x-date/index.js"></script>
<span id="out"></span>
You need to use javascript Date() object and method of it to getting current date and component of it.
var monthName = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var dateStr = date.getDate() + "th " + monthName[date.getMonth()] + " " + date.getFullYear();
$("#currentdate").text(dateStr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="currentdate"></div>
I am working in MVC and I have to bind dropdown values to an array in javascript. How do I do that?
dropdown list-
Html.DropDownListFor(m => m.MyActivityRequest.ToMonth, Model.MonthNames, new { #id = "monthToSelect" })
Javascript function:
$(document).ready(function(){
$("#yearToSelect").change(function(){
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var monthVar = date.getMonth();
var yearVar = date.getFullYear();
if ($(this).val() < yearVar ){
$("#monthToSelect").find('option').remove();
for( i = 0; i < 12; i++ ){
$("#monthToSelect").append($("<option>",{
value: months[i],
text: months[i]
}));
};
}
you can see that the the array- 'months' is hard coded as of now. I want to dynamically bind the dropdown values to this array.
Js Fiddle Demo.
Inside your code place the below code appropriately.
Better Approach:
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var option = '';
for (i=0;i<months.length;i++){
option += '<option value="'+ months [i] + '">' + months [i] + '</option>';
}
$('#monthToSelect').append(option);
Refered from here.
I think you have to pass the month list from controller. So in javascript you can make an ajax call and return the same list of months. You can use this list instead of array- 'months' in your javascript function.
How I make a sintax to convert date format from default format (yyyy-mm-dd) to english format like December 11th, 2013 using Javascript function?
Thank you
You could use moment.js
Moment.js 2.7.0
Moment was designed to work both in the browser and in Node.JS.
Parse, validate, manipulate, and display dates in javascript.
and it is also available on cdnjs.com.
var str_date = "1983-24-12",
arr_date = str_date.split('-'),
months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var new_date=months[arr_date[2]] + ' ' + arr_date[1] + ', ' + arr_date[0]);
http://jsfiddle.net/TN7NE/
Edit
You can add an ordinal using the following:
function addOrdinal(n) {
var ord = [,'st','nd','rd'];
var a = n%100;
return n + (ord[a>20? a%10 :a] || 'th');
}
e.g.
addOrdinal(1); // 1st
You can use a function like this:
function formatDate(date) {
months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'];
dateSplit = date.split('-');
year = dateSplit[0];
month = months[parseInt(dateSplit[1]) - 1];
day = parseInt(dateSplit[2]);
switch(day) {
case 1:
case 21:
case 31:
day += 'st';
break;
case 2:
case 22:
day += 'nd';
break;
case 3:
case 23:
day += 'rd';
break;
default:
day += 'th';
}
return month + ' ' + day + ', ' + year;
}