Posts

Get Time Stamp from datetime - javascript

Image
This code return a datetime stamp string, can be used to file name to make it differnet to avoid override of file. function   timeStamp () {      var   date  =  new   Date ();      var   year  =  date . getFullYear ();      var   month  =  date . getMonth () +  1 ;       var   day  =  date . getDate ();      var   hour  =  date . getHours ();      var   minutes  =  date . getMinutes ();      var   secconds  =  date . getSeconds ()      var   formatted  =  month  +  ''  +  day  +  ''  +  year  +  ''  +  hour  +  ''  +  minutes  +  ''  +  secconds ;     ...

format time 12 to 24 hours - javascript

Image
This code will return 24 hours time format form 12 hours function   format12to24 ( time ) {      var   t  =  time ;      if  (! t ) {  return   null ; }      var   arr  =  t . split ( ":" );      var   h  =  parseInt ( arr [ 0 ]);      if  ( t . includes ( "pm" )) {          var   nH  =  h  !=  12  ?  h  +  12  :  h ;          var   t  =  t . replace ( h ,  nH );     }      else  {          var   nH  =  h  !=  12  ?  h  :  h  -  12 ;          var   t ...

complex filter with some method to filter out nested object data.

Image
This is example of complex filter with some method to filter out nested object data.   let   a  = [{  name :   'hugh'  }, {  name :   'wyne'  }, {  name :   'tony'  }]; let   a1  = [{  name :   'john'  }, {  name :   'stark'  }, {  name :   'god'  }]; let   a2  = [{  name :   'tony'  }, {  name :   'stark'  }, {  name :   'wyne'  }]; let   b  = [{  bname :   'bname' ,  a :   a  }, {  bname :   'bname' ,  a :   a1  }, {  bname :   'bname' ,  a :   a2  }]; let   r2  =  b . filter ( x   =>   x . a . some ( g   =>  {      console . log ( 'some' ,  g . name  ===  'hugh' );    ...

Get extension of a file

Image
This code return extension of a file , just pass the file name with extension and then it use the array pop method to extract the extension from file name. example:  extension ( "myvideo.mp4" ) output : mp4 function   extension ( filename ) {      let   ex  = (  filename  &&  filename . split ( '.' ). pop ());      console . log ( ex );     return ex; } This one is very useful when your code requirement is to get extension of file. There's lots of approach to achieve the same result some programming language has predefined methods for the same. But this will help us to think ourselves and create the approach for our code problem. So here is an approach for this Approach 1. Create a method return extension of file. We using argument to be passed in the method that is filename which is string type. 2. There is lots methods available with string type variable to we going ...

Convert an url to blob url object

Image
This piece of code could be used to convert a URL to blob URL and get file in blob       var   xhr  =  new   XMLHttpRequest ;      xhr . responseType  =  'blob' ;      xhr . onload  =  function  () {          var   recoveredBlob  =  xhr . response ;           var   blobUrl  =  URL . createObjectURL ( recoveredBlob );     };      xhr . open ( 'GET' ,  'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvqNf3kxEV4wZGwGscm3r82l87kFfVBFeWuhCfrAnQbDtnE4kZUgxhxbqLXHWMXT83uKM98w77D8izfL6U4Y2RKtZKMkRNfc2pKHMaRJ7U07rEn1WOeH-WJOXcSSSvXxRj2e6Vw9yeVsM/s1600/1595756336067022-0.png' );      xhr . send (); This is a simple example but very useful when we need a file with blob type. In this example we just conve...

Calculate one hour addition to start date time for end date time

Image
     Calculate 1 hour form a date , application of this code where we want to set one hour from start date for calculation of end date . for example :             we have a case where we want to set start date for an event so the start date is = 11:10 am then this code calculate one end date which will be 12:10 pm you can use  dateTimeC  or  dateTimeC2  both has same result .    function   dateTimeC ( date ) {          var   hours  =  date . getHours ();          var   hours  = ( hours  +  24 ) %  24 ;          var   mid  =  'AM' ;          if  ( hours  ==  0 ) {              hou...

Time duration Counter format (00:00:00) for stopwatch , media recording etc..

Image
   This piece of code could be used to show time counting like when we recording something with media recording and want to show a time counter to indicate how much recording has been recorded in hours, minute and second, in format (00:00:00) time duration .      function   getTimeCount () {      var   startDateTime  =  new   Date ();      var   startStamp  =  startDateTime . getTime ();      var   newDate  =  new   Date ();      var   newStamp  =  newDate . getTime ();      return  ( cb )  =>  {          var   formatTimeCounter  = ( val )  =>  {           return   val  =  val  <  10  ?  `0 ${ val } `  :...