0

I want to get Date in the for following format 16-SEP-16 12.00 in javascript. I can't quite figure out how to achieve this. I need to send this as a query parameter for a REST API request.

Currently all I can think of this to format the date individually like

var date = new Date();
var dt = date.getUTCDate() + '-' + date.getUTCMonth() + '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);

I have a problem with it that the month is a number and not a shorthand like SEP.

Also is there any simple way to achieve this.

Thanks for help in advance.

Shubham Khatri
  • 211,155
  • 45
  • 305
  • 318
  • There's not really a shorthand like in PHP and SQL but there are methods to return each part of the date in just about any formatyou could want: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – I wrestled a bear once. Nov 02 '16 at 04:24
  • @Pamblam the problem is that I have a oracle DB which uses the shorthand of month and from frontend I need to send the same format as the query parameter. Also, I followed the same link that u suggested to achieve what I have done till now – Shubham Khatri Nov 02 '16 at 04:27
  • two types of solution provided hope u like it – dhruv jadia Nov 02 '16 at 04:43
  • @RobG I would have written my own answer whereby I used toLocaleString() even before you marked it a duplicate. I accepted the current answer because it was better suited between the three. – Shubham Khatri Nov 03 '16 at 05:19

3 Answers3

1

var monthNames = [
  "Jan", "Feb", "Mar",
  "Apr", "May", "Jun", "Jul",
  "Aug", "Sep", "Oct",
  "Nov", "Dec"
];


var date = new Date();
var dt = date.getUTCDate() + '-' + monthNames[date.getUTCMonth()] + '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);

without using month array you can use below solution

var date = new Date();
Date.prototype.monthName = function() {
    return this.toUTCString().split(' ')[2]
};
var dt = date.getUTCDate() + '-' + date.monthName()+ '-' + (date.getUTCFullYear() - 2000) + ' ' + date.getUTCHours() + '.' + date.getUTCMinutes();
console.log(dt);
document.write(dt);
dhruv jadia
  • 1,687
  • 2
  • 14
  • 28
  • This is an answer, but I don't like to prefer this – Shubham Khatri Nov 02 '16 at 04:31
  • @ShubhamKhatri : answer updated without using month array – dhruv jadia Nov 02 '16 at 04:42
  • The string returned by *toUTCString* is implementation dependent, therefore to assume that it will contain the month name in a particular place is not a good idea. – RobG Nov 02 '16 at 05:40
  • Thanks for the solution. – Shubham Khatri Nov 02 '16 at 05:51
  • @ShubhamKhatri—the "without month array" part is not a good solution. It depends on *toUTCString* to return the date string in a format that is implementation dependent, that is, the string can be returned in any format that the developer thinks is convenient. It may not contain the month name at all, or it might be in a different part of the string. Also, using UTC methods may not be appropriate. A format like "16-SEP-16 12.00" would be expected to be local. – RobG Nov 03 '16 at 05:02
  • @RobG If you have any Ideas on how can I proceed differently, I am happy to change my approach – Shubham Khatri Nov 03 '16 at 05:05
  • @ShubhamKhatri—see the marked duplicate, which has 43 answers. – RobG Nov 03 '16 at 05:06
1

If you dont have any problem using third party library, you can use Moment.js,it is a beautiful library for dates and formatting

In moment.js,you can format it as the following

var day = moment("2016-11-01");
console.log(day);
console.log(day.format("DD-MMM-YYYY HH:MM"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.js"></script>

Hope this helps

Geeky
  • 6,986
  • 2
  • 19
  • 48
  • I don't have any problem using a third party library but it would be better if I can achieve it in javascript alone – Shubham Khatri Nov 02 '16 at 04:33
  • Since I had to format date at multiple places in my project. I finally resorted to using moment.js. Thanks for help. I accepted you answer – Shubham Khatri Nov 10 '16 at 09:25
1

Insomnia made me write this..

It adds a format method to the date object without requiring any 3rd party libraries and returns a formatted date from the given shorthand.

I based it off PHP's date shorthand, which you can see for reference.

Date.prototype.format = function(format) {
  var months = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ];
  var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  format = format.replace(/y/g, ("" + this.getFullYear()).substring(2));
  format = format.replace(/Y/g, "" + this.getFullYear());
  format = format.replace(/m/g, ("00" + (this.getMonth() + 1)).substr(-2, 2));
  format = format.replace(/F/g, months[this.getMonth()]);
  format = format.replace(/M/g, months[this.getMonth()].substring(0, 3));
  format = format.replace(/n/g, "" + (this.getMonth() + 1));
  format = format.replace(/t/g, "" + new Date(this.getFullYear(), this.getMonth() - 1, 0).getDate());
  format = format.replace(/D/g, days[this.getDate()].substr(0, 3));
  format = format.replace(/d/g, ("00" + this.getDate()).substr(-2, 2));
  format = format.replace(/j/g, this.getDate()+"");
  format = format.replace(/l/g, days[this.getDate()]);
  format = format.replace(/w/g, this.getDay());
  format = format.replace(/a/g, this.getHours() > 11 ? "pm" : "am");
  format = format.replace(/A/g, this.getHours() > 11 ? "PM" : "AM");
  format = format.replace(/g/g, "" + (this.getHours() > 11 ? this.getHours() - 11 : this.getHours() + 1));
  format = format.replace(/G/g, "" + (this.getHours() + 1));
  format = format.replace(/h/g, ("00" + (this.getHours() > 11 ? this.getHours() - 11 : this.getHours() + 1)).substr(-2, 2));
  format = format.replace(/H/g, ("00" + (this.getHours() + 1)).substr(-2, 2));
  format = format.replace(/i/g, ("00" + this.getMinutes()).substr(-2, 2));
  format = format.replace(/s/g, ("00" + this.getSeconds()).substr(-2, 2));
  return format;
};

Examples..

// 11/02/16 2:17 AM
var d = (new Date()).format("m/d/y g:i A");

// November 2, 2016, 2:17 am
var d = (new Date()).format("F j, Y, g:i a")

And a fiddle

I wrestled a bear once.
  • 19,489
  • 16
  • 63
  • 110
  • In what circumstance would `"00" + this.getDate()` be required? There should be a test for invalid date like: `if (isNaN(this)) return this.toString()`. ;-) – RobG Nov 02 '16 at 05:45
  • putting 2 zeros in front of it and then getting a substring of the 2 right chars is just an easy way to pad the zeros to the number. try it and you'll see. in what circumstance will a date object ever be NaN? @RobG – I wrestled a bear once. Nov 02 '16 at 05:49
  • Thanks for your help. It will be useful to refer in future but certainly not for the present work. – Shubham Khatri Nov 02 '16 at 05:52
  • @RobG https://jsfiddle.net/9bbhsggs/ – I wrestled a bear once. Nov 02 '16 at 05:53
  • @ShubhamKhatri - If this is all for oracle you're really probably going about it wrong. Let oracle 's TO_DATE dothis for you, just pass the utc time to the server. This should not be done on the client side. – I wrestled a bear once. Nov 02 '16 at 05:55
  • Is it wrong to do this on client side, because I plan on taking the date as input form the user so, I thought better do it on the client side then on the server – Shubham Khatri Nov 02 '16 at 06:06
  • gnerally it is bad practice to prepare things for the database on the client side as client side data is easily altered by attackers. formatting a date is obviously not the worst thing you could do on the client side but best practice isto do it on the back end. in this case diong it on the back also happens to be much easier :) – I wrestled a bear once. Nov 02 '16 at 11:24
  • 1
    @Pamblam—there is no need to use more than "0" since the *get\** methods always return at least one character. A date will have a time value of NaN if it was created using an invalid or un–parseable string or set to an invalid value, so the *get\** methods will return "NaN" and you'll get "aN" for padded values. – RobG Nov 03 '16 at 01:05