Change a string to date with particular format? - javascript

I have a string like "2016-04-13". I need to change it to a date format like
"13 April, 2016". How can I do this in JavaScript?
What I've tried:
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

The solution using String.replace function and predefined month names list:
var dt = new Date("2016-04-13".replace("-","/")),
monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var dateStr = dt.getDate() +" "+ monthNames[dt.getMonth()] +", "+ dt.getFullYear();
console.log(dateStr); // 13 April, 2016

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();

Displaying my date to a specific format

I am having a little difficulty with this.How can I display a date value to format 10 September 2014 using Javascript.
What I am doing at the moment is
var dateAdded = new Date(parseInt(user.DateAdded.substr(6)));
which gives me Wed Sep 10 2014
duplicate of How to format a JavaScript date
but Marko's accepted answer there for you:
Taken from http://www.webdevelopersnotes.com/tips/html/javascript_date_and_time.php3:
<script type="text/javascript">
<!--
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "-" + m_names[curr_month]
+ "-" + curr_year);
//-->
</script>
You can edit the array to use Jan, Feb, Mar, etc..

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);

breaking up a string in jQuery

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.

Categories

Resources