Reinier
Reinier

Reputation: 215

Converting string to specific time format (string) JavaScript

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?

Upvotes: 0

Views: 67

Answers (5)

adeneo
adeneo

Reputation: 318352

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;

Upvotes: 1

Arnauld
Arnauld

Reputation: 6139

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);

Upvotes: 2

sielakos
sielakos

Reputation: 2404

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() );

Upvotes: 0

VincenzoC
VincenzoC

Reputation: 31502

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>

Upvotes: 0

Rajesh
Rajesh

Reputation: 24955

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>

Upvotes: 0

Related Questions