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 } `  :...

How can I use a MediaRecorder object in an Angular application?

Image
MediaRecorder is javascript element comes with native javascript, if your angular application does not show MediaRecorder then you can try any step from below Step1. Install type using this command this will install type for MediaRecorder but at compile time it will show error     npm   install  - D  @ types / dom - mediacapture - record before compile use this syntax to get rid from above error;     declare   var   MediaRecorder :  any ; If you get error after declaring   declare   var   MediaRecorder :  any ; that is "global is not defined" , then add this line to polyfills.js file ( window   as   any ). global  =  window ; Icon credit http://www.myiconfinder.com/icon/commands-media-record-recording-sound-music-voice-song-media-player-button-buttons-android-default-color-music-iphone/726 Angular

How to stream object from AWS S3 to browser using node and angular

Image
1. Create an api get request to node application.   eg:    Get '/api/get-file/:id' 2. Install packages aws-sdk , or express npm i express aws-sdk , leave it if pre- installed. 3. Follow the below code snippet  Aws S3 service provide getObject and createReadStream method. Pass the Params to s3.getObject then use createReadStream. use pipe method in createReadStream and pass the response (res) object to pipe method so it will pipe your stream to response. You can also use file write Stream location to save Stream in file.   var   app  =  require ( 'express' )();   var   aws   =  require ( 'aws-sdk' );   aws . config . update ({  region :   'us-east-1'  });   var   s3  =  new   aws . S3 ({ apiVersion :   '2006-03-01' });   const   BUCKET  =  '<your bucket name>' ;       app . get ( '/api/ge...