Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove comma between date and time on toLocaleString() in Js

var date ="03/05/2013";
var localDate = date.split("/").reverse().join("-");
var localTime = "20:41"
var UTCDateTime = localDate+ "T" + localTime +":00.000Z";
localDateTime = new Date(UTCDateTime)

var options = { hour12: false, day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute:'2-digit' };
console.log("Date:>>"+localDateTime.toLocaleString('en', options));

How do you remove comma after date Date:>>05/03/2013 21:41

like image 917
user2235768 Avatar asked Apr 23 '18 13:04

user2235768


People also ask

What does the toLocaleString () method do in JS?

The toLocaleString() method returns a Date object as a string, using locale settings. The default language depends on the locale setup on your computer.


3 Answers

What's about using regex?

'01/01/2018, 12:00'.replace(/^(\d{2})\/(\d{2})\/(\d{4}), (\d{2}):(\d{2})/, '$1/$2/$3 $4:$5')

var date            = '03/05/2013';
var localDate       = date.split('/').reverse().join('-');
var localTime       = '20:41';
var localDateTime   = new Date(localDate+ 'T' + localTime + ':00.000Z');

var options         = {hour12: false, day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute:'2-digit'};
var output          = localDateTime.toLocaleString('en', options);
var outputFormatted = output.replace(/^(\d{2})\/(\d{2})\/(\d{4}), (\d{2}):(\d{2})/, '$1/$2/$3 $4:$5');

console.log(outputFormatted);
like image 149
l2aelba Avatar answered Oct 07 '22 01:10

l2aelba


I wouldn't replace the comma with either the replace function or a regex, because that could easily result in the wrong commas being removed. The short date format that you use in your example doesn't contain a comma in the date part, but a longer date format like { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute:'2-digit' } will.

A more resilient option is to format the date and time parts separately, and concatenate them with a space in between:

var date ="03/05/2013";
var localDate = date.split("/").reverse().join("-");
var localTime = "20:41"
var UTCDateTime = localDate+ "T" + localTime +":00.000Z";
localDateTime = new Date(UTCDateTime)

var dateOptions = { day: '2-digit', month: '2-digit', year: 'numeric' };
var timeOptions = { hour12: false, hour: '2-digit', minute:'2-digit' };
console.log("Date:>>" + localDateTime.toLocaleDateString('en', dateOptions) + " " + localDateTime.toLocaleTimeString('en', timeOptions));
like image 35
Jesse van Assen Avatar answered Oct 06 '22 23:10

Jesse van Assen


With replace:

var date ="03/05/2013";
var localDate = date.split("/").reverse().join("-");
var localTime = "20:41"
var UTCDateTime = localDate+ "T" + localTime +":00.000Z";
localDateTime = new Date(UTCDateTime)

var options = { hour12: false, day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute:'2-digit' };
console.log("Date:>>"+localDateTime.toLocaleString('en', options).replace(',',''));
like image 38
Douwe de Haan Avatar answered Oct 07 '22 01:10

Douwe de Haan