simplest way to convert MySql timestamp to month/year with javascript - 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);

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

Coverting DATETIME string to time and date in words Javascript or jQuery

i have a datetime parameter in a string from an JSON object
like this-
datetime:"2015-04-15 09.00.00"
my goal is to convert it to:
"15 April 09:00"
(2015 can be included as well but not necessary)
After i will append in in 2 different places, date and time.
Any suggestions? Can't seem to find any good info apart from getting a plugin.
You can parse the month like the answer here:
https://stackoverflow.com/a/1643468/2498251
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
so for your case you can make something like this:
var d = new Date("2015-04-15 09:00:00");
var day = d.getDay();
var month = monthNames[d.getMonth()];
var year = d.getYear();
var hour = d.getHours();
var min = d.getMinutes();
var fullDatetime = day + ' ' + month + ' ' + hour + ':' + min;

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

How to display dates using prompts in 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;
}
}

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