breaking up a string in jQuery - javascript

I have this string...
var myDate = "November 15, 2010";
.....and I need to do this...
$("#request_showdate_1i").val('2010');
$("#request_showdate_2i").val('November');
$("#request_showdate_3i").val('15');
Any ideas how to break this string up?

You can for example use a regular expression:
var parts = /(\S+) (\d+), (\d+)/.exec(myDate);
$("#request_showdate_1i").val(parts[3]);
$("#request_showdate_2i").val(parts[1]);
$("#request_showdate_3i").val(parts[2]);

You can use the Date object, example on jsFiddle
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var d = new Date("November 15, 2010");
$("#request_showdate_1i").val(d.getFullYear()); // 2010
$("#request_showdate_2i").val(months[d.getMonth()]); // November from array
$("#request_showdate_3i").val(d.getDate()); // 15

#Guffa's answer seems worth a try, but have you thought about using the datepart features of Javascript?
3 divs to hold the different date parts
<div class="day"></div>
<div class="month"></div>
<div class="year"></div>
And some Javascript magic
var myDate = new Date("November 15, 2010");
var month_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
$(".day").text(myDate.getDate());
$(".month").text(month_names[myDate.getMonth()]);
$(".year").text(myDate.getFullYear());
Here's a jsFiddle sample.

Related

How can change the format of Date in javascript?

I have a date like 2019-02-01. how can I change it to February 2019?
You can just do this:
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date("2019/02/01");
const dateString = `${monthNames[d.getMonth()]} ${d.getFullYear()}`;
console.log(dateString);
This will return the month in English, no matter the system's language.
Note that you should use new Date("2019/02/01") (with slashes) to specify a date in your timezone. If you use new Date("2019-02-01"), then it will assume UTC time, and weird stuff will happen (see this answer for more info on this).
const d = new Date("2019/02/01");
const m = d.toLocaleString('default', { month: 'long' });
console.log(m, d.getFullYear());
/* you can use the following code to convert date to the required format. */
var todaysDate = new Date();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var formatted_date = months[todaysDate.getMonth()] + " " + todaysDate.getDate() + " " + todaysDate.getFullYear();

How to get previous month name on given some month (in words not numerical value)? [duplicate]

This question already has answers here:
How to get previous month (DECEMBER) name
(6 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I want a function that gives the full name of prev month for any given month[not just current month]
Ex: prevMonth(August) = July; prevMonth(January)= December.
I'm new to js and can't figure out how to use this array to get the result:
monthsarray: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
Are expecting a simple solution like this?
var monthsarray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
function prevMonth(curMonth) {
var curMonthIndex = monthsarray.indexOf(curMonth);
var prevMonthIndex;
if (curMonthIndex == 0) prevMonthIndex = monthsarray.length - 1;
else if (curMonthIndex == monthsarray.length - 1) prevMonthIndex = 0;
else prevMonthIndex = curMonthIndex - 1;
return monthsarray[prevMonthIndex];
}
console.log(prevMonth("August"));
console.log(prevMonth("December"));
let monthsarray1= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] ;
let m="May";
console.log(pvsMonth(m))
function pvsMonth(m){
let ln=monthsarray1.length
let idx=monthsarray1.indexOf(m)
if(idx==0){
return monthsarray1[ln-1]
}else{
return monthsarray1[idx-1]
}
}
function prevMonth(month) {
const index = monthsarray.indexOf(month);
if (index > 0) {
return monthsarray[index - 1];
}
if (index === 0) {
return monthsarray[monthsarray.length - 1];
}
throw new Error('Invalid Month given');
}

what is the meaning of use two bracket in javascript array

I want to change the date format in javaScript and I find this code of "Mike Christensen" that is very nice.
But my question is about what is meaning of use two bracket in javascript array code at var month line 3.
var mydate = new Date(form.startDate.value);
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()];
var str = month + ' ' + mydate.getFullYear();
He is indexing the month array using the month number that he gets it from mydate.getMonth()
Here in this code ,mydate.getMonth() is 11 and as is he indexing month array month[11] would be december
var mydate = new Date();
console.log(mydate.getMonth());
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()];
console.log(month);
var str = month + ' ' + mydate.getFullYear();
Hope you understand
The first pair of brackets is creating of array, consider it like this
var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
The next pair of brackets is a retrieving operation for the array by index.
someArr[index]
in your case
month[11]
That's it, the author just take an element of the just created array by index, where index is extracted from another variable.
doing this: is a very cheap way to init the var month with only elements in the array declared previously...(something that will save you to write one line more but IMHO decrease the readability of the code... )
so breaking that down:
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()];
is the same as
var monthOptionsArray = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var monthIndex = mydate.getMonth();
var monthActual = monthOptionsArray[monthIndex];
Example:
var mydate = new Date();
console.log(mydate.getMonth());
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]; monthI = month[1];
console.log(monthI);
var str = month + ' ' + mydate.getFullYear();

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...

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