I got this value from a MySQL DB field: "september 9 # 08:00 - 17:00".
Can i change this client side to 9 september? I tried with JavaScript but i didn't come even close.
The date may change.
Desired result in browser: 9 september
Thank you,
That's a pretty uncommon output for a datetime value from the database. One solution is to simply strip all text after the # character - if you're sure the output format stays as is:
var datetime = 'september 9 # 19:20 - 20:00';
var result = datetime.substr(0, datetime.indexOf('#')).trim();
console.log(result); // outputs 'september 9'
var date = new Date(result);
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
console.log(date.getDate(), months[date.getMonth()]); // outputs '9 September'
The values you want are in the string, so just reformat it:
var s = 'September 9 # 08:00 - 17:00';
var b = s.split(' ');
console.log(b[1] + ' ' + b[0]);
// You could even do
console.log(s.replace(/(\w+) (\d+)(.*)/, '$2 $1'));
// Or if you want to just swap the month and day and keep the rest
console.log(s.replace(/(\w+) (\d+)/, '$2 $1'));
Related
I am trying get last six months date from current date .
var d = new Date();
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
alert(months[d.getMonth()-6]);
but i am getting udefined
I always recommend using date libraries to assist in these types of calculations. One of the most popular with a ton of support and examples is moment.js (https://momentjs.com/)
To get six months ago from the current date using moment is:
moment().subtract(6, 'months')
and then to print the month name would be:
moment().subtract(6, 'months').format('MMMM')
You have to write your code like below-
var d = new Date();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
d.setMonth(d.getMonth() - 6);
console.log(months[d.getMonth()]);
We need to get Month from date object the set to back (with - 6) then get it back.
var d = new Date();
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var monthName = months[new Date(d.setMonth(d.getMonth() - 6)).getMonth()];
console.log(monthName)
I'm trying to write a script that prints the names of the previous 12 months. Since this month is January, it should print: December
November
October
September
August
July
June
May
April
March
February
January
Instead, it prints March twice. http://jsfiddle.net/h69gm04g/2/
November
October
September
August
July
June
May
April
March
March
February
HTML
<div id="test"></div>
Javascript
monthNames = [ "January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December" ];
d = new Date();
for (i = 0; i < 12; i++) {
d.setMonth(d.getMonth() - 1);
monthName = monthNames[d.getMonth()];
$('#test').append(monthNames[d.getMonth()] + "<br>");
}
What am I doing wrong?
Nice one! Took me a while.
The reason for this is that today is the 29th. Since your date object is set to the current day implicitly and February only had 28 days in 2013, you see March printed twice. Fix this by setting the optional day parameter:
d.setMonth(d.getMonth() - 1, 1);
That's because today happens to be the 29th, and when you get to february the 29th it will wrap over into march.
Set the date to the 1st (or any other date that is less than 29), then it works for all months:
d = new Date();
d.setDate(1);
d.setMonth(d.getMonth() - 1);
Demo: http://jsfiddle.net/h69gm04g/3/
I don't think there's any reason for answers that manipulate a Date object on each iteration. Once we know the current month, all we need to do is iterate the list backwards, wrapping around at the end. That's a job for %. Unfortunately, % does not do a true mathematical mod operation, and can return a negative value, so the easiest implementation is to ensure that the value is positive by adding an additional 12 to it:
var month = new Date().getMonth();
for (i = 1; i <= 12; i++) {
$('#test').append(monthNames[(12 + month - i) % 12] + "<br>");
}
(JSFiddle)
Try this http://jsfiddle.net/oLp9hegk/:
monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
m = (new Date()).getMonth();
for (var i = 0; i < 12; i++) {
$('#test').append(monthNames[(m-i+11)%12] + "<br>");
}
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);
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!!
Writing a JavaScript program in Google Apps Scripts, I'm encountering datetime strings that look like 2nd line in
//German
März 1, 2013 # 19:00 - 23:00
//"March 1, 2013 # 19:00 - 23:00"
This needs to be parsed to two JS date objects (d1 = March 1, 2013 19:00; d2 = March 1, 2013 23:00)
What is the best way to do this in the Google Apps Scripts environment?
For now I have written a custom function lookupMonth() to translate from German to English. I do this string.replace(monthGer, monthEng).
function monthLookup(str) {
//str1 = str.substring(0, str.indexOf(" ")); //unicode blank!
var pos = str.indexOf(/[0-9]/.exec(str)); //pos of first digit
str1 = str.substring(0, pos - 1 ).trim();
var lookup = {
"Januar": "January",
"Februar": "February",
"März": "March",
"April": "April",
"Mai": "May",
"Juni": "June",
"Juli": "July",
"August": "August",
"Spetember": "Septemper",
"Oktober": "October",
"November": "November",
"Dezember": "December"
};
str = lookup[str1] + " " + str.substring(pos, 1000);
return str;
}
This creates two new strings which can be to a Date() object, and then I could call Utilities.formatDate() Is there a better way?
Should I, for instance, call the translation service API (which would of course be much slower than calling an in-process JavaScript function?
Should I import a third-party library? If so, which one?