4

I'm using JavaScript and JQuery and I've got a string like this (unchangeable xml response):

var str = "2017-01-08T16:06:52+00:00";

How can I convert to a Date like this:

08 january 2017, 16:06:52

Or at least:

08 01 2017, 16:06:52

I was trying to use a .replace() like:

str = str.replace(/(\d{4})-(\d{2})-(\d{2})T(\d{8}).*/,'$2 $3 $1, $4');

But it doesn't works. :(

5
  • 1
    new Date("2017-01-08T16:06:52+00:00").toLocaleString() or maybe new Date("2017-01-08T16:06:52+00:00").toGMTString().split(/\W/) to get the fancy names without a list Commented Jan 8, 2017 at 16:44
  • @dandavis while much simpler, unfortunately that will use the date format specified on the client machine. If the OP requires that the date is always shown in the format they specified they will need to hand-code the formatting. Commented Jan 8, 2017 at 16:45
  • @RoryMcCrossan: i also showed how to use GMT to get the parts without a whitelist of names... with the parts array, custom formats are trivial. Commented Jan 8, 2017 at 16:46
  • I know, but it's still using the format of the client machine. The output of the above is an array of Sun,,08,Jan,2017,16,06,52,GMT which I suspect isn't what you intended either. Commented Jan 8, 2017 at 16:48
  • @RoryMcCrossan: i get the EXACT same parts because GMTString is standardized, unlike localeString... using the parts, one can make a custom format: something LIKE p[1]+", " + p[3]+":" +p[5]+":"+p[2]; or whatever... just another option ;) Commented Jan 8, 2017 at 16:49

3 Answers 3

6

To do this you can create a Date() object from the string, then concatenate together a string from the methods the Date() object exposes. Try this:

var str = "2017-01-08T16:06:52+00:00";
var date = new Date(str);
var months = [ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" ];

var dateString = ("00" + date.getDate()).slice(-2) + ' ' + months[date.getMonth()] + ' ' + date.getFullYear() + ', ' + ("00" + date.getHours()).slice(-2) + ':' + ("00" + date.getMinutes()).slice(-2) + ':' + ("00" + date.getSeconds()).slice(-2);

console.log(dateString);

Note that this can be made simpler by using a date formatting library (such as MomentJS or DateJS) however including an entire library to format a single date is rather wasteful. If you need to do this repeatedly throughout your site then they may be worth it.

Also note that in English speaking countries the month names should be capitalised, so consider amending the format for that case.

Sign up to request clarification or add additional context in comments.

Comments

0

Your regular expression isn't quite right, \d{8} will match 8 consecutive digits but there are colons in there as well, you need something like:

var str = "2017-01-08T16:06:52+00:00"
str = str.replace(/(\d{4})-(\d{2})-(\d{2})T(.{8}).*/, '$2 $3 $1, $4');
console.log(str)

To replace the month number with the name is just a little more work:

function reformatDate(s) {
  var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
  var b = s.split(/\D/);
  return b[2] + ' ' + months[b[1]-1] + ', ' + b[0] + ' ' + b[3] + ':' + b[4] + ':' + b[5];
}
  
  
var s = "2017-01-08T16:06:52+00:00"  
console.log(reformatDate(s));

Be careful using the Date constructor (and Date.parse) for parsing strings, it's largely implementation dependent.

If you want to present the date and time in the timezone of the client, then parse the string to a Date, then return it as a string. A library like fecha.js is good for that. It's small and just does parsing and formatting, e.g.

var s = '2017-01-08T16:06:52+00:00'
var d = fecha.parse(s,'YYYY-MM-DDTHH:mm:ssZZ');

console.log(fecha.format(d,'dd MMMM, YYYY HH:mm:ss'));

Comments

0

Alternatively, if adding a library is an option you could use moment.js. You can call .format and pass how you would like the output to be, for example:

var str = "2017-01-08T16:06:52+00:00";
var momentDate = moment(str);

console.log(momentDate.format('DD MMMM YYYY HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>

1 Comment

moment(new Date()) produces the same result as moment() but is more to type. ;-) If using moment.js, it should use it for parsing too,and since the OP's string is in an ISO 8601 format moment(str) will do the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.