How to display dates using prompts in JavaScript? - javascript

I am trying to insert dates in the prompt boxes using JavaScript. How do I do it?
I have tried to insert month and date using prompt window separately using two prompt windows.

JavaScript prompt method accepts strings as input. You can use the date object, perform some string formatting and pass it to the prompt object and it should work as shown below:
Demo
JavaScript:
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function myFunction() {
var today = new Date();
var date = prompt("Please enter date.", today.getDate()+"-"+monthNames[today.getMonth()]+"-"+today.getFullYear());
if (date != null) {
x = "Hello! You have entered date as: " + date;
document.getElementById("demo").innerHTML = x;
}
}

Related

Format array elements from one form to another -JS

I have an array of strings that are actually dates in the form of 201001 ,201002. I would like to convert those array items to something like 2010 January, 2010 February. Is there any way to do this.
var Array = ["201005", "201006", "201007", "201008", "201009", "201010", "201011", "201012", "201101", "201102", "201103", "201104", "201106", "201107", "201108", "201109", "201110", "201111", "201112", "201201", "201202", "201203", "201204", "201205", "201206", "201207", "201208", "201209", "201210", "201211", "201212", "201301", "201302", "201303", "201304", "201305", "201306", "201307"];
I'm looking for an array like :
var expected = ["2010 january", "2010 February" etc]
You could do this:
const monthMap = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December"
};
xAxisArray = xAxisArray.map(axis => {
const year = axis.substring(0, 4);
const month = axis.substring(4, 6);
return `${year} ${monthMap[month]}`;
});
Or you could use moment, but that might be overkill.
You can try my code
function formatDate(date) {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var monthIndex = date.getMonth();
var year = date.getFullYear();
return year + ' ' + monthNames[monthIndex];
}
var result = xAxisArray.map(item => {
var strDate = item.slice(0, 4) + '-' + item.slice(4, 6) + '-01'
return formatDate(new Date(strDate))
})
console.log(result)
This is a demo: https://codepen.io/phuongnm153/pen/xxKgKYp
You can try the following code:
var xAxisArray = ["201005", "201006", "201007", "201008", "201009", "201010", "201011", "201012", "201101", "201102", "201103", "201104", "201106", "201107", "201108", "201109", "201110", "201111", "201112", "201201", "201202", "201203", "201204", "201205", "201206", "201207", "201208", "201209", "201210", "201211", "201212", "201301", "201302", "201303", "201304", "201305", "201306", "201307"];
//Array to link month number to name
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
//Result array
var formattedArray = [];
//Loop through the old array and push the formatted date to the new array
xAxisArray.forEach(function(element) {
var formatted = element.substr(0,4) + '-' + element.substr(4); //Add the dash between year and month
var date = new Date(formatted); //Create date object
var year = date.getFullYear(); //Get the year
var month = date.getMonth(); //Get the month
formattedArray.push(year + ' ' + monthNames[month]);
});
console.log(formattedArray);
First, create a date from the string. Then convert that date to the desired format and last add the new formatted string to a new array.
If you want to have the full name of the month, and not just the first three letter, you need to have them somewhere, for example:
const MONTHS = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
Then you can simply have:
const format = (value) => {
// assuming the format is 4 digits followed by 2 digits
const [, year, month] = value.match(/(\d{4})(\d{2})/);
return `${year} ${MONTHS[month - 1]}`
}
Then you can map your array such as:
const expected = xAxisArray.map(format);
Hope it helps!
You can get your answer by following approach .
xAxisArray.forEach(function(value , index){
var last2 = value.slice(-2);
const date = new Date(value.slice(4), last2); // 2009-11-10
const month = date.toLocaleString('default', { month: 'long' });
xAxisArray[index] = value.substring(0,4) +" "+ month;
});
Your problem will get solve .
You can use moment library for this:
const result = xAxisArray.map(item => {
const year = item.substring(0,4)
const month = item.substring(4,2)
return moment(`01/${month}/${year}`).format('YYYY MMMM')
})
console.log(result)
By using moment.js libary you can convert , use below expression convert it
moment("your value").format("YYYY MMM");
ex:moment(201005).format("YYYY MMM");

How to increment textbox (month)name on button click in javascript

I want to increase and decrease a text box name on click button
here an image i post to give clarification of what i have to do exactly
i tried but text value increaseing but i wana show month name in that text box
Any help is appreciated.
You could try with Array .create the array for month .And apply the increment and decrements value with array arguments position.Ternary operator used for restrict the count below 0 and above 11
var month = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var c =0;
$('#mins').click(function(){
c = c-- > 0 ? c-- : 0;
$('#mnt').val(month[c])
})
$('#plus').click(function(){
c = c++ < 11 ? c++ : 11;
$('#mnt').val(month[c])
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="mnt" value="January">
<button id="mins">-</button>
<button id="plus">+</button>
Create an Array of month names, a starting variable to represent the current location in the Array, and a variable to hold the input field for the Month:
var months = ["January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"];
var currentMonth = 0;
var monthInput = $("#month-input");
Now add click events to the + and - buttons. You'll need to add an id attribute to each one.
$("#subtract-month-button").click(subtractMonth);
$("#add-month-button").click(addMonth);
Finally, your methods to handle the click events.
function subtractMonth () {
if (currentMonth > 0) {
currentMonth--;
}
monthInput.val(months[currentMonth]);
}
function addMonth () {
if (currentMonth < months.length - 1) {
currentMonth++;
}
monthInput.val(months[currentMonth]);
}

How to format date like "July 14, 2014" in javascript/jQuery? [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
I'm getting date value from jQuery BeatPicker like "14/07/2014", I need to format this value as "July 14, 2014".
Is there any easy way to achieve this using javascript/jQuery/plugin?
Thanks
If you wanted to stick with pure Javascript
function formatDate(dateString){
var monthNames = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var dateArray = dateString.split("/");
var month = monthNames[dateArray[1]-1];
var day = dateArray[0];
var year = dateArray[2];
return month + " " + day + ", " + year;
}
var text = "14/07/2014";
var output = text.replace(/(\d\d)\/(\d\d)\/(\d\d\d\d)/, function($0, $1, $2, $3) {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return monthNames[parseInt($2, 10) - 1] + " " + $1 + ", " + $3;
});
Totally untested, and yes - the regex is messy (should use curly brackets however this reads more easily), but should work about right...

simplest way to convert MySql timestamp to month/year with javascript

I get this value from MySQL: 2014-01-11 14:11:10
I would like to take that value and format it like so: Jan 2014
What is the simplest way to do so?
You can instantiate the Date object with the string given, like
var date = new Date("2014-01-11 14:11:10");
and then manipulate with that object. Unfortunately, there is no built-in way to transform it to the format you wish. However you can do this on your own.
In other words, you could do something like
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var dateString = "2014-01-11 14:11:10";
var date = new Date(dateString);
var msg = m_names[date.getMonth()] + " " + date.getFullYear();
alert(msg);

Is there a JQuery API for assigning the month name based on the number.?

Am working on SharePoint List, fetching the date from the list, which is generally in this format : 2013-03-25.
I have split the date and assigned to an variable.
Code.
var splitDate = resultRegionArr[x].NewsDate.split("-");
Now, i want to know if there is any Jquery API which returns me the string "March" on input of value "03" or splitDate[1].
Thanks in advance.
var months = [ "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" ];
var selectedMonthName = months[splitDate[1] - 1];
The - 1 is optional. Depending on your input/output of splitDate[1].
try
www.datejs.com/, for example:
alert(Date.parse('2013-03-25').toString('dd MMMM yyyy')) // 25 March 2013
this will give you what you want also bonus is other features!!

Categories

Resources