This question already has answers here:
JavaScript convert string into Date with format (dd mmm yyyy) i.e. 01 Jun 2012
(6 answers)
Closed 8 years ago.
I have a date in the format yyyymmdd.
Ex: 20141004
What I need to do is to convert it like dd-mmm-yyyy.
Ex: 4 Oct 2014
There is a similar question here: JavaScript convert string into Date with format (dd mmm yyyy) i.e. 01 Jun 2012
The difference is that I have no "/" character to split by.
Another limitation is that I cannot use any external library, as this code is used inside another tool, so I need native JS.
I did it like this:
var date = "20141004";
var y = date.substring(0, 4);
var m = parseInt(date.substring(4, 6), 10);
var d = date.substring(6, 8);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
mm = months[m - 1];
document.write(d + " " + mm + " " + y);
Related
i have two dates Date1 and Date2 in format ("Wed Apr 21 2020") .I want to compare only months from two date strings.Forex ample Date1="Fri Sep 13 2020" and Date2="Sun Feb 21 2020" and now i want to compare September from DATE1 with February from DATE2 in such a way .
if(September>February){
var X=greater
}else{
var X=smaller
}
how can i achieve this in JAVASCRIPT
The easiest way is to extract the month from the string and get its index by using an array. See an example below:
const Date1 = 'Fri Sep 13 2020';
const Date2 = 'Sun Feb 21 2020';
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const getMonthIndex = date => months.indexOf(date.split(' ')[1]);
if (getMonthIndex(Date1) > getMonthIndex(Date2)) {
// var X=greater
console.log('greater');
} else {
// var X=smaller
console.log('smaller');
}
I have a date format that is like this
"5-2015"
How can I convert it so that it appears as "May 2015" on screen?
You could try MomentJS http://momentjs.com/ which is a Date/Time library for Javascript. I believe the syntax would be moment(yourDate, 'M-YYYY').format('MMM YYYY');
If you want to roll your own:
function format(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var month = date.substring(0, date.indexOf('-'));
var year = date.substring(date.indexOf('-') + 1);
return months[parseInt(month) - 1] + ' ' + year;
}
var formatted = format('5-2015');
Assuming all of your dates are in the format of "5-2015" (e.g. 6-2015 or 12-2015), what you can do is use the split function on javascript split the string value into months and years.
For example:
var date = "5-2015";
date.split("-"); //splits the date whenever it sees the dash
var month;
if(date[0] == "5"){ //accesses the first split value (the month value) and check if it's a month.
month = "May";
} //do this with the rest of the months.
var finalString = month + " " + date[1]; //constructs the final string, i.e. May 2015
alert(finalString);
You can go like this:
function parseDate(dateString) {
var d = dateString.split("-"); // ["5", "2015"]
// Month is 0-based, so subtract 1
var D = new Date(d[1], d[0]-1).toString().split(" "); // ["Fri", "May", "01", "2015", "00:00:00", "GMT+0200", "(Central", "Europe", "Daylight", "Time)"]
return D[1] + " " + D[3];
}
(function() {
alert(parseDate("5-2015"));
}());
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
I need your help,
how can I rework and restring a date string from yyyy-mm-ddd to dd/mm/yyyy?
Example: 2014-06-27, firstly replace the dash with a slash, then shift the order of the digits around to form 27/06/2014
I am not sure as to how to go about doing this?
Thanks
I've made a custom date string format function, you can use that.
var getDateString = function(date, format) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
getPaddedComp = function(comp) {
return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
},
formattedDate = format,
o = {
"y+": date.getFullYear(), // year
"M+": months[date.getMonth()], //month
"d+": getPaddedComp(date.getDate()), //day
"h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
"H+": getPaddedComp(date.getHours()), //hour
"m+": getPaddedComp(date.getMinutes()), //minute
"s+": getPaddedComp(date.getSeconds()), //second
"S+": getPaddedComp(date.getMilliseconds()), //millisecond,
"t+": (date.getHours() >= 12) ? 'PM' : 'AM'
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
formattedDate = formattedDate.replace(RegExp.$1, o[k]);
}
}
return formattedDate;
};
And now suppose you've :-
var date = "2014-06-27";
So to format this date you write:-
var formattedDate = getDateString(new Date(date), "d/M/y")
if you're using a string, then the string.split would be an easy way to do this.
C# code:
public void testDateTime()
{
string dashedDate = "2014-01-18"; // yyyy-mm-dd
var stringArray = dashedDate.Split('-');
string newDate = stringArray[2] + "/" + stringArray[1] + "/" + stringArray[0];
//convert to dd/mm/yyyy
Assert.AreEqual(newDate, "18/01/2014");
}
I have this String :
var str = "Thu, 10 Apr 2014 09:19:08 +0000";
I would like to get this format : "10 Apr 2014"
How can i do that?
var str = "Thu, 10 Apr 2014 09:19:08 +0000";
var d = new Date(str);
var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var b= d.getDate()+' '+month[d.getMonth()]+' '+d.getFullYear();
alert(b);
Check the result in
JSFiddle
You can split the string on spaces and take the second to the fourth items and join:
var d = str.split(' ').slice(1, 4).join(' ');
Demo: http://jsfiddle.net/Guffa/7FuD6/
you can use the substring() method like this,
var str = "Thu, 10 Apr 2014 09:19:08 +0000";
var res = str.substring(5,15);
var str = "Thu, 10 Apr 2014 09:19:08 +0000",
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
d = new Date(str);
d.getDate() + " " + months[d.getMonth()] + " " + d.getFullYear(); //"10 Apr 2014"
The date string you have can be passed into the Date constructor to get a date object, d. The date object has various methods to that gives day, year, month, time etc. Since months are returned as an integer and we need the name, we use an array called months.
This date converter function working fine on chrome but not on Firefox! any one can help figure out problem.
function converter(string) {
var d = new Date(string);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'sep', 'Oct', 'Nov', 'Dec'];
var hour = d.getHours();
return d.getFullYear() + ' ' + months[d.getMonth()] + ' ' + d.getDate() + ' | ' + (hour % 12) + ' ' + d.getMinutes() + ':' + (hour > 11 ? 'pm' : 'am');
}
alert(converter('2013-03-10 19:43:55'))
This will fix your code:
converter('2013-03-10T19:43:55')
Please note the T between the date and the time.
Edit: The datetime string should be an ISO 8601 format. Read more about it here.
Chrome is more forgiving of bad date formats, but that's technically not valid as input to Date.parse or new Date. You should make sure that it's a correctly formatted date (RFC2822 or ISO 8601), or if you want to allow more freeform input, use a library like http://www.datejs.com/
More information: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse