Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

How do you get a timestamp in programming language?


Get time timestamp in Java:
first import the file
import java.sql.Timestamp;
          import java.util.Date;

and, then just defined following code

         java.util.Date date= new java.util.Date();
      System.out.println(new Timestamp(date.getTime()));

Get timestamp in Java script:

the following returns the number of milliseconds since the epoch.

     new Date().getTime();

Or on browsers that support ES5 (notably not IE8 and earlier), you can use Date.now:

      Date.now();

which is really easily shimmed for older browsers:

      if (!Date.now) {
        Date.now = function() { return new Date().getTime(); };
    }

Get timestamp in IOS:


NSString * timestamp = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];

(times 1000 for milliseconds, otherwise, take that out)

If You're using it all the time, it might be nice to declare a macro

#define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]

Then Call it like this:

NSString * timestamp = TimeStamp;

Or as a method:

- (NSString *) timeStamp {
    return [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
}



Get timestamp in PHP:

here is the function get timestamp in php:

<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
?>


Get timestamp in C#:

Function that creates a timestamp in c#

public static String GetTimestamp(this DateTime value)
{
    return value.ToString("yyyyMMddHHmmssffff");
}

Get timestamp in Python:

here is the simple way to get time in python;

Python 2.7.3 (default, Apr 24 2012, 00:00:54) 
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> ts = time.time()
>>> print ts
1355563265.81
>>> import datetime
>>> st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
>>> print st
2012-12-15 01:21:05
>>>

Get timestamp in Perl:

getting time in perl for this way:


If you want to control the format of the timestamp, I usually throw in a subroutine like the following. This will return a scalar in the format of "20120928 08:35:12".

sub getCurrentTime {

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    my $nice_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",
                                   $year+1900,$mon+1,$mday,$hour,$min,$sec);
    return $nice_timestamp;
}

Then change your code to:

my $timestamp = getCurrentTime ();



Happy Coding!!!









Validate email in JavaScript?


This is one of those useful things that people will be Googling for validate email address on Javascript, this tutorial mostly used when you are developing app on Cross platform, like sencha, phonegap.

function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);

}

Here, Using regular expressions is probably the best way. Here's an example (live demo):


Happy Coding!!!