0

I have a string of datetime that looks like this:

2017-04-17 18:26:03

I can use javascript to reformat and shorten it like so:

var input = '2017-04-17 18:26:03';
var result = input.replace(/^(\d+)-(\d+)-(\d+)(.*):\d+$/, '$3/$2/$1');

console.log(result);

/////Prints This: 17/04/2017///

Now, I need to make it even shorter like this:

17/04/17

Could someone please advice on this?

Any help would be appreciated.

EDIT: I don't want to use anything like moment.js or any other library as it is overkill.

David Hope
  • 1,426
  • 4
  • 21
  • 50
  • 2
    You should get acquainted with https://momentjs.com/. – ceejayoz Apr 24 '17 at 14:50
  • 1
    Do not use regex for this. https://learn.microsoft.com/en-us/scripting/javascript/date-and-time-strings-javascript#formatting-dates – Brennan Apr 24 '17 at 14:51
  • possible duplicate of [how to get current date format in dd/mm/yyyy](http://stackoverflow.com/questions/12409299/how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-i) – Felix Haeberle Apr 24 '17 at 14:52
  • 2
    "EDIT: I don't want to use anything like moment.js or any other library as it is overkill." https://en.wikipedia.org/wiki/Not_invented_here – ceejayoz Apr 24 '17 at 14:57
  • 2
    "I don't want to use anything like moment.js or any other library as it is overkill". Depends how long you want to spend smoothing out the issue with your own method. This stuff is, unfortunately, a pain. These people have solved it already, so why not use the library? If you wanted to save a single text file in C# or PHP, you wouldn't write your own File I/O library because using the built-in ones is "overkill", would you? – ADyson Apr 24 '17 at 15:03
  • @ADyson, I don't want to use moment.js mainly because I am working on a project which is all about speed. so to load another external library or a heavy js file like moment.js will cause it to slow down thus not wanting to use it. hope that clears the air about moment.js – David Hope Apr 24 '17 at 15:06
  • TIL 16kb is "heavy". – ceejayoz Apr 24 '17 at 15:08
  • @ceejayoz, OMFG, do you guys get paid from moment.js for fighting their corner?!? YES, it is HEAVY when the whole bloody project is all 13kb... – David Hope Apr 24 '17 at 15:09
  • @DavidHope Nope, Moment doesn't pay me a cent. Twenty years of experience in webdev has just taught me that fear of third-party libraries and a few extra KB is a silly thing that winds up wasting significant amounts of time. – ceejayoz Apr 24 '17 at 15:13
  • @ceejayoz, I wouldn't call 16kb "a few extra kb" when the whole project is just 13kb. – David Hope Apr 24 '17 at 15:15
  • @DavidHope If I have $5 in my wallet, $5 more is still just a few dollars. I'm not suddenly rich. – ceejayoz Apr 24 '17 at 15:17
  • @DavidHope unless your users are still on dial-up I doubt they will notice the difference. Most modern sites include mountains of JS, far more than 30KB total. No-one's being paid by momentJS, they just think it will save you hours and hours of unpleasant debugging. Especially next time you want to do something with dates, apart from this one. FWIW, just to prove I'm no lackey, here are some alternatives https://www.quora.com/What-is-the-best-JavaScript-date-formatting-library – ADyson Apr 24 '17 at 15:18
  • @ceejayoz, your example is irrelevant! With your $5 in your pocket, you are not dealing with internet speed, users device processors etc etc... Not to mention what this project is about. without getting into too much details, I have given you enough answers to recognize that its not always as back and white as you think it is.. – David Hope Apr 24 '17 at 15:22
  • @ADyson, this is not the first time that I am working with datetime and wont be the last either. I have used moment.js in a large PHP project and it worked fine. However, in this project, i need to stay away from it because of speed and size issue (that's if you want to call it an issue). – David Hope Apr 24 '17 at 15:24
  • 1
    @DavidHope if that's what is appropriate for your project, then of course you're free to do whatever you think best. But please don't take offence at people trying to offer the benefit of their experience. Obviously we can't know the full context of your operating constraints, so sorry if our assumptions are incorrect. It's just that our experience of working with dates across multiple devices and browsers is that it's messy and inconsistent, and a library where someone's worked out all the kinks already has proved very useful. If something else works for you, then by all means use it. – ADyson Apr 24 '17 at 15:30
  • 1
    @ADyson, I didn't take offence at anyone! I just said I don't want to use their suggestion and they seem to get offended by me saying that and getting all sarcastic with me by repeating my words and putting them in quotes. I appreciate the help and suggestions but people need to stop being so pushy and if someone doesn't accept their suggestion then that shouldn't offend them. at the end of the day, everyone has a reason for something that they say and do. – David Hope Apr 24 '17 at 15:35
  • 2
    @DavidHope no problem. People do get a bit brash on this site sometimes, and if I'm guilty of it as well then I apologise. I certainly didn't realise you'd used it before and had good reasons not to do so here. I have encountered people who make up arbitrary rules about not using libraries, or are afraid of new things. So if I was pushy it was only because I wouldn't want someone to miss out on something that would help them due to misguided assumptions. As you've clearly and articulately elaborated, this isn't the situation you are in. Good luck with your project. – ADyson Apr 24 '17 at 15:39
  • This is a pretty good alternative to moment: https://date-fns.org/docs/format If used with a proper bundler, you only include the functions that you use. – Balázs Édes Apr 24 '17 at 17:03

2 Answers2

2

You're right, moment.js is overkill to do it. But I'm not sure regex are the fastest way to do that and it isn't the most readable. Moreover Date object has methods to do it. For example you can use toLocaleDateString() :

const date = new Date('2017-04-17 18:26:03');

const formattedDate = date.toLocaleDateString("en-GB", {
    year: "2-digit",
    month: "numeric",
    day: "numeric"
});

console.log( formattedDate ); // "17/04/17"

And if you are concerned about performance, when formatting a lot of dates, it is better to create an Intl.DateTimeFormat object and use the function provided by its format property :

const date = new Date('2017-04-17 18:26:03');

const dateFormatter = new Intl.DateTimeFormat("en-GB", {
    year: "2-digit",
    month: "numeric",
    day: "numeric"
});

const formattedDate = dateFormatter.format(date);

console.log( formattedDate ); // "17/04/17"
Damien
  • 3,915
  • 1
  • 17
  • 18
1

If you're using a regex, you can just take the year modulo 100 when building the string:

let match = input.match(/^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)$/)
let result = `${match[3]}/${match[2]}/${match[1] % 100}`
console.log(result)
//=> '17/4/17'
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Honestly this has nothing to do with local or UTC. this is just a string that will never change which I need to make shorter.. so the local or UTC scenario wont even come into this occasion. – David Hope Apr 24 '17 at 15:08
  • This is strange. this code works fine in browser on a computer but doesn't work in an Android device! – David Hope Apr 24 '17 at 18:31
  • Maybe try it with `var` instead of `let`? Although your original code has template strings; if those work, I'd expect `let` to as well. – Mark Reed Apr 24 '17 at 21:25