0

I need to convert a string from a html data tag to a specific time format in JavaScript.

var time = "August 01, 2016 01:30" 

needs to become

"01 August 2016 01:30:00"

The following code does that:

var time = "August 01, 2016 01:30"
var time_array = time.split(",")
var month = time_array[0].split(' ')[0]
var day = time_array[0].split(' ')[1]
var year = time_array[1].split(' ')[1]
var time = time_array[1].split(' ')[2]+ ":00"
var result = day + " " + month + " " + year + " " + time

But I wondered if there is a more efficient, faster way to do this?

1
  • Have you considered using the momentjs library for this? momentjs.com Commented Jul 27, 2016 at 10:38

5 Answers 5

2

Assuming that you don't need to check whether the input string is describing a valid date and time, you can simply use the .replace() method:

var time = "August 01, 2016 01:30";

time = time.replace(/([a-z]+) (\d{2}), (\d{4} \d{2}:\d{2})/i, "$2 $1 $3:00");

console.log(time);

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

Comments

1

If you don't want to include an entire library, and want to still work with actual dates, it's not that hard to parse the date yourself

var time = "August 01, 2016 01:30";

var months = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
];

var parts = time.split(/[\s,:]/);
var date  = new Date(parts[3], months.indexOf(parts[0]), parts[1], parts[4], parts[5]);

Now that you have a date object, you can output anything you'd like, and you could use the months array to get the month back etc.

function pad(x) { return x < 10 ? '0' + x : x}

var new_date = [
    pad(date.getDate()), 
    months[date.getMonth()], 
    date.getFullYear(), 
    pad(date.getHours()) + ':' +
    pad(date.getMinutes())  + ':' +
    pad(date.getSeconds())
];

var parsed = new_date.join(' ');

var time = "August 01, 2016 01:30";

var months = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December'
];

var parts = time.split(/[\s,:]/);
var date  = new Date(parts[3], months.indexOf(parts[0]), parts[1], parts[4], parts[5]);

function pad(x) { return x < 10 ? '0' + x : x}

var new_date = [
    pad(date.getDate()), 
    months[date.getMonth()], 
    date.getFullYear(), 
    pad(date.getHours()) + ': ' +
    pad(date.getMinutes())  + ': ' +
    pad(date.getSeconds())
];
                    
var parsed = new_date.join(' ');

document.body.innerHTML = parsed;

Comments

0

As suggested by @andrey-etumyan, you should use moment.js

var time = "August 01, 2016 01:30" 
var d = moment(time, "MMMM DD, YYYY hh:mm")
console.log(d.format("DD MMMM YYYY hh:mm:ss"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>

Comments

0

Using momentjs, you can have the following code:

var time = "August 01, 2016 01:30";
time = moment(time, 'MMMM DD, YYYY HH:mm').format('DD MMMM YYYY HH:mm:ss');
console.log(time)//"01 August 2016 01:30:00"
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>

Comments

0

You can do that easily without any momentjs or manual parsing string. Date will parse it nicely.

var date = new Date("August 01, 2016 01:30");
var months = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"];


console.log(date.getDate() + ' ' + months[date.getMonth()] + ' ' +
            date.getFullYear()  + ' ' + date.getHours() + ':' + date.getMinutes() );

Comments

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.