0

How would I go about converting following date:

Thu Feb 18 12:25:00 SGT 2016

into a format like '2016-02-18'?

I know,

  1. That, using a new Date(Date.parse('...')), with calls, will help me get it. But the problem being timezone part (SGT).
  2. I dont want to use any libraries out there.

What would be the effective way to go about this?

Any help is appreciated!

PS:

Ok, I have tried

new Date(Date.parse('Thu Feb 18 12:25:00 SGT 2016'))

but of-course, it is going to me 'invalid date' error.

MaDHaN MinHo
  • 529
  • 7
  • 23
Jeremy Rajan
  • 662
  • 3
  • 12
  • 1
    Why not just read [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)? You **cannot** parse such strings by built-in means. The only way is manual parsing – hindmost Feb 03 '16 at 09:05
  • @hindmost, totally agree. I have gone through them and i know that is not the right technique. That is why, I am curious to know the effective way. – Jeremy Rajan Feb 03 '16 at 09:08
  • No library required: split into parts, convert month name to number, arrange in the right order and join with "/". Only needs 3 lines of code. – RobG Feb 03 '16 at 09:09
  • See if it helps you http://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object – Siddhartha Chowdhury Feb 03 '16 at 09:11
  • @SiddharthaChowdhury—that would require parsing the string to Date first, which is more code than required. – RobG Feb 03 '16 at 09:12
  • @RobG, thanks! But provided that, the source string sticks to same format right? – Jeremy Rajan Feb 03 '16 at 09:15

4 Answers4

2

I guess I should wait for you to post some code, but here's an example. It splits the string into parts, converts the month name to a number, then outputs the bits you want in the order you want. The slice on the month name is just in case you want to use the full month name, but maybe that's unnecessary:

function reformatDate(s){
  var b = s.split(/[\s:]/);
  var months = {jan:'01',feb:'02',mar:'03',apr:'04',may:'05',jun:'06',
                jul:'07',aug:'08',sep:'09',oct:'10',nov:'11',dec:'12'};
  return b[7] + '/' + months[b[1].toLowerCase().slice(0,3)] + '/' + ('0'+b[2]).slice(-2);
}

document.write(reformatDate('Thu Feb 18 12:25:00 SGT 2016'));

That format date string (y/m/d) isn't consistent with any standard that I know of and likely will not be parsed by most browsers.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks @RobG, that does look like a good solution. I am gonna give it a shot, with my function as I have to support multi-language as well. Thanks mate! Gonna wait till, if any more solutions come in, if any :). Its not for the browser, its for the systems; as it only accepts that format (YYYY-mm-dd) – Jeremy Rajan Feb 03 '16 at 09:26
0

Why not just take the individual components of the date object and build a string from them?

full_date = new Date();
formatted_date_string = full_date.getFullYear() + "-" + (full_date.getMonth()+1) + "-" + full_date.getDate();

I'll leave it to you to sort out the leading 0s on the day and month.

ElPedro
  • 576
  • 10
  • 17
0

var input="Thu Feb 18 12:25:00 SGT 2016";
  
var ar=input.split(" ");

  var months = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"};

console.log(ar[ar.length-1]+"-"+months[ar[1]]+"-"+ar[2]);

  

Try this code no date parsing required. Please check on console for result.

MaDHaN MinHo
  • 529
  • 7
  • 23
0

var date = "Thu Feb 18 12:25:00 SGT 2016";

date = date.replace("SGT","GMT+0800") // replaces SGT with GMT+0800 because 
singapore time is 8 hrs ahead than GMT timezone

var newdate = new Date(Date.parse(date))

var requiredDate = newdate.toLocaleDateString()
  • 1
    Basically convert your string to valid date format and then parse it to the date object! – Manoj Shevate Feb 03 '16 at 17:21
  • @Buddhabhushan Kamble, but what if there is some other string, other than SGT? Because time zones can differ right. Unless ofcz, we use a regex or something to take it out. – Jeremy Rajan Feb 04 '16 at 01:09
  • @Jeremy Yes, you are right but if we don't replace timezone string like SGT, it will gives the time of SGT if someone from other country like India will get the time of singapore. – Buddhabhushan Kamble Feb 04 '16 at 05:52
  • @Jeremy Yes, you are right but if we don't replace timezone string like SGT, it will gives the time of SGT if someone from other country like India will get the time of singapore. AS per your regex it will give the time of the SGT if someone want the time of his own timezone then it won't work. For example . Thu Feb 04 00:25:00 SGT 2016 in this if someone from India wants the actual time as per your regex the date will be 2016-02-04 but actually in India date should be 2016-02-03. So its good to convert first in GMT and then parse so that you will get the correct date. – Buddhabhushan Kamble Feb 04 '16 at 06:04