vignesh's Profile

2062
Points

Questions
83

Answers
200

  • Asked on January 2, 2019 in PHP.

    The solution is very simple,

    $seconds = time();
    $rounded_seconds = round($seconds / (15 * 60)) * (15 * 60);
     
    echo "Original: " . date('H:i', $seconds) . "\n";
    echo "Rounded: " . date('H:i', $rounded_seconds) . "\n";
    

    The code above can be used for both original and rounded value.

    We can also use floor() function instead of round() which is used for round it down.

    • 447 views
    • 3 answers
    • 0 votes
  • Here is the procedure,

    The number of whole milliseconds since 1970-01-01T00:00:00Z is 1382086394000 . This value is about the time . By using the Date constructor we can create ECMAScript Date object with the help of the time value.

    var d = new Date(1382086394000);
    

    We should send the output for call the input in string method. This method gives us the human readable form of time known as equivalent system time.

    For example,

    Fri Oct 18 2013 18:53:14 GMT+1000 (EST)
    

    There are lot of built-in formatting options in In ES5, some examples are,

    • toDateString
    • toTimeString
    • toLocaleString

    For more reference about these build in formats and ECMAScript Date object .

    And one important thing is, we have to format the date ourself if we really want to it is works in all types of browsers.

    alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear());
    
    • 397 views
    • 3 answers
    • 0 votes
  • One can achieve the solution by using this code ,

    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2010-07-14 09:00:02");
    String time = new SimpleDateFormat("H:mm").format(date);
    
    • 433 views
    • 5 answers
    • 0 votes
  • With the help of JavaDoc, this code should helps.

    @Deprecated
    public int getYear()
    

    Deprecated is one that programmers are discouraged from using, typically because it is dangerous, in JDK version 1.1 it was replaced by Calendar.get(Calendar.YEAR) – 1900.

    It yields that value is, from the year that contains subtract by 1900  or starts with the immediate in time represented by this as interpreted in the local time zone Date object.

    The output is here:   represented by this date, the year should be minus 1900.

    For more reference : Calendar

    So the output is 112, so try to use javadoc or javatime.

    • 453 views
    • 5 answers
    • 0 votes
  • A function named DAY_OF_YEAR can be used for find the number of years between two dates.

    public static int getDiffYears(Date first, Date last) {
        Calendar a = getCalendar(first);
        Calendar b = getCalendar(last);
        int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR);
        if (a.get(Calendar.DAY_OF_YEAR) > b.get(Calendar.DAY_OF_YEAR)) {
            diff--;
        }
        return diff;
    }
     
    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance(Locale.US);
        cal.setTime(date);
        return cal;
    }
    

    We have to diff the milliseconds of the time and divide it by the number of milliseconds in a whole year. Maintain these things long most of the time but it is not constant, it is depends on the application for the number of years.

    • 427 views
    • 5 answers
    • 0 votes
  • Everything that date related topics in java is comes under joda.time library.

    In this question we have to used the function in joda.time library called  Years.yearsBetween() .

    • 427 views
    • 5 answers
    • 0 votes
  • Asked on December 28, 2018 in Ruby.

    Simply use this one line code,

    Date.new(2014,10,1).next_month.prev_day
    
    • 480 views
    • 6 answers
    • 0 votes
  • Here is the solution,

    The chances are  get hamstrung especially, In case we are working in Rails, or if  we switch among Time, Date, and DateTime, especially when it comes to use or deal with UTC/time zones, daylight savings, and the like.

    It’s best to use Time, and stick with it anywhere.

    We use, there are two good ways, depending on context:

    use the class method on Time If we really want  month m and year y,

    days = Time.days_in_month(m, y)
    
    

    Incase we need  a Time object t, cleaner to ask the day number of the last day of the month, the final month’s dat.

    days = t.end_of_month.day
    
    
    • 826 views
    • 12 answers
    • 0 votes
  • Asked on December 28, 2018 in Bash.

    Use this code,

    #since this was yesterday
    date -dyesterday +%Y%m%d
     
    #more precise, and more recommended
    date -d'27 JUN 2011' +%Y%m%d
     
    #assuming this is similar to yesterdays `date` question from you
    #http://stackoverflow.com/q/6497525/638649
    date -d'last-monday' +%Y%m%d
     
    #going on @seth's comment you could do this
    DATE="27 jun 2011"; date -d"$DATE" +%Y%m%d
     
    #or a method to read it from stdin
    read -p " Get date >> " DATE; printf " AS YYYYMMDD format >> %s" `date
    -d"$DATE" +%Y%m%d`
     
    #which then outputs the following:
    #Get date >> 27 june 2011
    #AS YYYYMMDD format >> 20110627
     
    #if you really want to use awk
    echo "27 june 2011" | awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash
     
    #note | bash just redirects awk's output to the shell to be executed
    #FS is field separator, in this case you can use $0 to print the line
    #But this is useful if you have more than one date on a line
    

    For more information about date.

    The code above works on only for GNU date.

    sunfreeware.com version of date is support for -d, not Solaris version of date

    • 619 views
    • 12 answers
    • 0 votes
  • Have a look at the code,

    Calendar currentDate = Calendar.getInstance(); //Get the current date
    SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss"); //format it as per your requirement
    String dateNow = formatter.format(currentDate.getTime());
    System.out.println("Now the date is :=> " + dateNow);
    
    • 428 views
    • 5 answers
    • 0 votes