Categories
JavaScript Open Source Solutions Web

JavaScript Date Format YYYY-MM-DD

Here is a simple function that takes the input of JS Date and returns a string of format YYYY-MM-DD:

var getDateFromDateTime = function(date) {
date = new Date(date); //Using this we can convert any date format to JS Date
var mm = date.getMonth() + 1; // getMonth() is zero-based
var dd = date.getDate();
if(mm<10){ mm="0"+mm; } if(dd<10){ dd="0"+dd; } return [date.getFullYear(), mm, dd].join('-'); // padding };

Categories
JavaScript Open Source Solutions Web

Convert JavaScript Date format YYYYMMDD

Here is a simple function that takes the input of JS Date and returns a string of format YYYYMMDD:

var getDateFromDateTime = function(date) {
date = new Date(date); //Using this we can convert any date format to JS Date
var mm = date.getMonth() + 1; // getMonth() is zero-based
var dd = date.getDate();
if(mm<10){ mm="0"+mm; } if(dd<10){ dd="0"+dd; } return [date.getFullYear(), mm, dd].join(''); // padding };

Categories
AngularJS Solutions Web

AngularJS – Filter to add Leading 0s to any Number

Lets say you want to display a list of numbers from 1 to n but want them to be displayed as 01 02 03 04 05… and not 1 2 3 4 5… Angular by default doesn’t have a filter for this.

Here is how you implement it:

Implement the filter in a module. Here we take the variable len to define how many 0s must be prepended to the number.
If len is 2 then 1 becomes 01
If len is 3 then 1 becomes 001
If len is 4 then 1 becomes 0001… so on and so forth

angular.module('myModule', [])
.filter('fixedLengthPrefix', function () {
return function (n, len) {
var num = parseInt(n, 10);
len = parseInt(len, 10);
if (isNaN(num) || isNaN(len)) {
return n;
}
num = ''+num;
while (num.length < len) { num = '0'+num; } return num; }; });

Now add the filter to the markup:

{{number | fixedLengthPrefix:2}}

Categories
Linux Solutions Ubuntu Web

WordPress: Error Uploading Media Files

I recently got the following error while uploading media to my wordpress site:

Unable to create directory uploads/2016/06. Is its parent directory writable by the server?

Here are the steps to fix the issue:

  • Possible Issue 1: Incorrect Directory pointed to in the settings:
    • Open your WordPress Admin page (this is usually at /wp-admin)
    • Hover over the sidebar menu “Settings” and then click on “Media
    • Check the section “Uploading Files“(If this section is missing then try the next solution)
    • check the value for “Store Uploads in This Folder
    • Most commonly the value should be “wp-content/uploads“, if not is should be pointing to the correct folder on the server. My setup had it pointing to the wrong directory with the wrong permissions.
  • Possible Issue 2: Wrong permissions
    • Login to your server via ssh or ftp
    • Traverse to the installation folder of the WordPress site
    • Check and see if the user:group settings are correct (Usually for UNIX systems its www-data:www-data)
    • If that is fine look at the permissions for the ‘uploads‘ directory and its sub directories
    • running a command ‘ls -l uploads‘, will give you the permissions set
    • The ideal permission is of 755. For this run the command “chmod -r 755 uploads

Hopefully that will solve your problems.

Categories
JavaScript Open Source Web

AngularJS – Round off Number to Fixed Length of Fractions

Angular’s built in number filter allows one to specify the number of decimals that should be displayed.

The syntax is as follows:
{{val | number:<count>}}

Where val is the value to be displayed, number is the filtername and count is the number of decimals to be displayed.

For eg.:

If val is 1234.56789

{{val | number:2}}

Displays: 1234.56

{{val | number:0}}

Displays: 1235

{{val | number:4}}

Displays: 1234.5679