-2

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,

prasanth
  • 22,145
  • 4
  • 29
  • 53

2 Answers2

0

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'
JasonK
  • 5,214
  • 9
  • 33
  • 61
  • Hi Jason K, Thank you for the respond. Yes, it is a string in a nvarchar field. But it's: september 9 @ 08:00 - 17:00 How can i switch the day and month with your code? – user6079228 Sep 04 '17 at 13:04
  • Hi thx, but that was not wat i was looking for. the value of the field can chacnge, you don't have to see it as a date. i simply want to swat the first two words. – user6079228 Sep 04 '17 at 16:01
  • The second output is `NaN undefined` in Safari. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Sep 05 '17 at 01:00
0

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'));
RobG
  • 142,382
  • 31
  • 172
  • 209