精华内容
下载资源
问答
  • 本文实例讲述了Python实现计算两个时间之间相差天数的方法。分享给大家供大家参考,具体如下: #-*- encoding:UTF-8 -*- from datetime import date import time nowtime = date.today() def convertstringtodate...
  • Excel计算天数的函数与公式总结

    千次阅读 2021-08-04 00:20:18
    内容提要:Excel计算天数的函数与公式总结。通常情况下,计算两个日期的天数,直接相减即可以。日期小常识:1900-1-0是虚拟的一个特殊日期,是时间日期的起点,对应序列值0。1900-1-1凌晨0点对应序列值1,也就是说每...

    内容提要:Excel计算天数的函数与公式总结。

    通常情况下,计算两个日期的天数,直接相减即可以。

    日期小常识:1900-1-0是虚拟的一个特殊日期,是时间日期的起点,对应序列值0。1900-1-1凌晨0点对应序列值1,也就是说每一天对应一个序列值单位1。1900-1-1正午12点对应序列值1.5,就是说时间间隔(小时、分、秒)也是同时间序列值一一对应的。于是1 1/3(1又三分之一)就对应1900-1-1上午6点(一日=24小时,24*1/3=8)。

    因此,很多对于日期的计算都可以转化为数值之间的计算或以此来理解。比如两个日期之间相隔的天数只要将两个日期相减就可以了。这是因为日期相减实质就是序列号相减,而序列号的单位1就等价为一天。

    掌握基本的时间日期函数DATE,date(year,month,day) 这三个参数能直接从日期参数中提取对应的年份、月份和所在月的第几天。

    WEEKDAY:返回对应星期几的数值,根据第二参数的不同意义不同。第二参数可以是1,2,3,建议记一个2就行。中国人的习惯星期日是一星期的最后一天,使用2作为第二参数正好符合这种习惯。

    TODAY,NOW函数,分别返回当前的日期和时间。

    VALUE,DATEVALUE函数,能将日期转化为序列值。

    第一:Excel中计算今天是本年(2011年)多少天

    1.SQL解法:select datepart("y",now()) as 第几天

    2. =TODAY()-"2011-1-1"+1或者TODAY()-DATE(2011,1,0)

    说明:使用当天日期和本年度第一天相减来获取一个数字,这个数字需要加1才能成为当天的序号。

    3. DATEDIF("2011-1-1",NOW(),"d")+1

    DATEDIF函数是计算两个日期之间的天数、月数或年数。DATEDIF函数是一个隐藏函数,在Excel的帮助文件查找不到相关的资料。这里提供一个:Excel中DATEDIF函数用法实例

    4.数组公式:=MATCH(TODAY(),DATE(YEAR(TODAY()),1,ROW(1:366)))

    第二,计算两个日期间的全部工作日数,除去周末的天数,可以使用 NETWORKDAYS函数。《networkdays函数的用法及实例》

    展开全文
  • 在Java中计算两个日期间的天数,大致有2种方法:一是使用原生JDK进行计算,在JDK8中提供了更为直接和完善的方法;二是使用第三方库。1、使用原生的JDKprivatestaticlongdaysBetween(Dateone,Datetwo){longdifference...

    在Java中计算两个日期间的天数,大致有2种方法:一是使用原生JDK进行计算,在JDK8中提供了更为直接和完善的方法;二是使用第三方库。

    1、使用原生的JDK

    private static long daysBetween(Date one, Date two) {

    long difference =  (one.getTime()-two.getTime())/86400000;

    return Math.abs(difference);

    }

    private static long daysBetween(Date one, Date two) {

    long difference = (one.getTime()-two.getTime())/86400000;

    return Math.abs(difference);

    }

    这种方式由于计算简单,为广大开发人员所采用。

    注意:由于转换成毫秒数计算,如果要获得较为准确的结果,应将日期规整,即将日期的时分秒设置为0:00点整点,避免2日期时间(时分秒)的差异造成计算偏差。

    参考代码:

    import java.text.SimpleDateFormat;

    import java.util.Date;

    public class DateTest {

    private int getTermDays2() throws Exception{

    String sEndDate = "2018-03-01";

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Date date1 = new Date();

    Date date2 = sdf.parse(sEndDate);

    long days = (date2.getTime() - date1.getTime()) / (24*3600*1000);

    long yushu = (date2.getTime() - date1.getTime()) % (24*3600*1000);

    System.out.println("days:" + days + ",yushu:" + yushu);

    //规整方法1

    date1.setHours(0);

    date1.setMinutes(0);

    date1.setSeconds(0);

    long days2 = (date2.getTime() - date1.getTime()) / (24*3600*1000);

    long yushu2 = (date2.getTime() - date1.getTime()) % (24*3600*1000);

    System.out.println("days2:" + days2 + ",yushu2:" + yushu2);

    //规整方法2

    String sdate1 = sdf.format(date1);

    date1 = sdf.parse(sdate1);

    long days3 = (date2.getTime() - date1.getTime()) / (24*3600*1000);

    long yushu3 = (date2.getTime() - date1.getTime()) % (24*3600*1000);

    System.out.println("days3:" + days3 + ",yushu3:" + yushu3);

    return (int)days;

    }

    public static void main(String[] args) {

    DateTest dt = new DateTest();

    try {

    dt.getTermDays2();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    import java.text.SimpleDateFormat;

    import java.util.Date;

    public class DateTest {

    private int getTermDays2() throws Exception{

    String sEndDate = "2018-03-01";

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Date date1 = new Date();

    Date date2 = sdf.parse(sEndDate);

    long days = (date2.getTime() - date1.getTime()) / (24*3600*1000);

    long yushu = (date2.getTime() - date1.getTime()) % (24*3600*1000);

    System.out.println("days:" + days + ",yushu:" + yushu);

    //规整方法1

    date1.setHours(0);

    date1.setMinutes(0);

    date1.setSeconds(0);

    long days2 = (date2.getTime() - date1.getTime()) / (24*3600*1000);

    long yushu2 = (date2.getTime() - date1.getTime()) % (24*3600*1000);

    System.out.println("days2:" + days2 + ",yushu2:" + yushu2);

    //规整方法2

    String sdate1 = sdf.format(date1);

    date1 = sdf.parse(sdate1);

    long days3 = (date2.getTime() - date1.getTime()) / (24*3600*1000);

    long yushu3 = (date2.getTime() - date1.getTime()) % (24*3600*1000);

    System.out.println("days3:" + days3 + ",yushu3:" + yushu3);

    return (int)days;

    }

    public static void main(String[] args) {

    DateTest dt = new DateTest();

    try {

    dt.getTermDays2();

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    规整方法1无法消除毫秒数的影响,结果差1天;规整方法1则可较好地消除时间对天数计算的影响。

    因此,计算2个日期间的差别时,若不希望计算时间差异,只计算天数,则一定要先对2个日期进行规整处理,然后再进行计算,否则得到的结果可能会有较大的误差。

    2、使用第三方库

    使用较多的类库为jodo-time。

    参考:

    If you are not running on Java 8, then there are two ways to calculate the difference between two dates in Java in days, either by using standard JDK classes e.g.java.util.Date andjava.util.Calendar or by using the joda-time library. Unfortunately, Java's old Date and Calendar API is buggy and not intuitive, so many of us by default use Joda for all date and time arithmetic. In this example, you will learn how to find  the number of days between today and any date entered by a user using Joda, as well as without using any third party library. When I first time face this problem, I thought what's a big deal about finding the difference between dates? If you can convert Date to milliseconds then finding a number of days, months or years are just a matter of simple arithmetic, but I was WRONG. I was not thinking about real world date and time nuisance like leap seconds, leap years, and daylight saving time.It's very difficult to accurately calculate the difference between two dates in Java without using third party library unless you have time to develop your own library like Joda, which diligently takes these things into consideration. Thankfully, you don't need to worry because Java 8 got lucky third time.There is a new Date and Time API which has corrected previous mistakes and turns out to be a real gem. If you are keen to learn Java 8, not just this API, I suggest you grab a copy of Java 8 in Action, one of the better books to learn new features of Java 8 in quick time.

    How to find number of days between two dates in Java

    Since java.util.Date class implementsComparable interface it's easy to figure out whether a date come before or after another date, or whether two dates are equal to each other as shownhere, but when it comes tofinding out how many days between two dates? we don't have a simple method likedaysBetween(date1, date2) in JDK library.

    Unfortunately, this is a quite common requirement, as you may need to find days between today and your next birthday, or how many days to your next insurance premium from today, or simply how many days between last two cricket world cups. In fact, calculating date and time difference is the most common date arithmetic in Java.In this article, we will see two ways to solve this problem, first by using JDK, without using any third party library and second by using the joda-time library.

    How to find the difference between two dates in Java?

    Though all is not lost, you can quickly write a simple routine to find the difference between two dates in terms of days in Java. How? By converting a date to milliseconds. Once you have milliseconds, you can just subtract them and then again divide them by86400000 (milliseconds per day). This way you will get exactly how many days between two dates in Java. How will you get millisecond from a date in Java? by usinggetTime() method ofjava.util.Date class, as shown below :

    private static long daysBetween(Date one, Date two) {

    long difference = (one.getTime()-two.getTime())/86400000;

    return Math.abs(difference);

    }

    This will work 99% of the time, but just like any quick and dirty solution, it will not handle any special cases e.g. time or timezone, leap years, or day light saving time. It will fail on summer boundaries when day light changes occur.

    A better solution is to use a tried and tested date and time library like Joda-Time, which handles those tricky scenarios much better. Alternatively, you can also use newjava.time classes from Java 8, which are heavily influenced from the joda-time library. SeeJava SE 8 for Really Impatient by Cay S. Horstmann for more details.

    BTW, if you are using JDK 8 then you and use java.time.Period class to calculate the difference between two dates in Java. Here is an example ofcalculating the date and time difference in Java 8, it's super easy and not error prone like earlier API. There is another class called,java.time.Duration, which is good to calculate a time difference between two instant.

    How to calculate days between two dates using joda-time in Java?

    In order to solve this problem using joda-time, we need to use a class called LocalDate to represent your dates. It is similar to LocalDate class of Java 8, or should I say LocalDate is inspired by this class.  This class knows about the weirdness we just talked about, e.g. some timezones don't have days that start at midnight. You can calculate difference between two dates in joda by using static utility class Days, which has method daysBetween() to return the number of days between two given dates as shown in the following example :

    public static int daysBetweenUsingJoda(Date d1, Date d2){

    return Days.daysBetween(

    new LocalDate(d1.getTime()),

    new LocalDate(d2.getTime())).getDays();

    }

    You can see that Days.daysBetween() method accepts a LocalDate and its's very easy to convert an instance of java.util.Date to org.joda.time.LocalDate class, just pass a number of milliseconds to it.

    Dependency for Joda Time library Joda-Time requires Java SE 5 or later and has no dependencies. There is a compile-time dependency on Joda-Convert, but this is not required at run-time thanks to the magic of annotations. You can downloadjoda-time-2.5.jar either from Maven central or directly from http://www.joda.org/joda-time/ website. If you are using Maven then you can also add the following dependency into your pom.xml file :

    joda-time

    joda-time

    2.5

    If you download JAR file then make sure you add joda-time-2.5.jar in your Java program's classpath and you are done.  If you are not sure how to do that, see thistutorial.

    Difference between two dates in number of Days

    Here is our full Java program to find out how many days between two dates in Java using standard JDK classes and by using open source Joda Time library. I have name aptly named our program "date diff", as its calculating difference between dates.

    import java.text.DateFormat;

    import java.text.ParseException;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    import java.util.Scanner;

    import org.joda.time.Days;

    import org.joda.time.LocalDate;

    /**

    * Java Program to find number of days between two dates in Java.

    * This program calculate difference between two dates in days using two ways

    * without using third party library and by using joda-time library.

    *

    * @author WINDOWS 8

    */

    public class DateDiffExample {

    private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd");

    public static void main(String args[]) throws ParseException{

    System.out.println("Please enter two dates in format yyyy/MM/dd to compare");

    Scanner reader = new Scanner(System.in);

    String first = reader.nextLine();

    String second = reader.nextLine();

    Date one = getDate(first);

    Date two = getDate(second);

    // quick and dirty way, work but not in all conditions

    // you can convert date into milliseconds then subtract

    // them and again convert it to days

    long numberOfDays = daysBetween(one, two);

    System.out.printf("Number of days between date %s and %s is : %d %n",

    first, second, numberOfDays);

    // a better way to calculate difference between two dates in Java

    // is by using JodaTime library as shown below

    int differenceBetweenDates = daysBetweenUsingJoda(one, two);

    System.out.printf("difference betweeen two dates %s and %s is : %d %n",

    first, second, differenceBetweenDates);

    reader.close();

    }

    /*

    * Simple way to parse String to date in Java

    */

    private static Date getDate(String date) throws ParseException{

    return df.parse(date);

    }

    /*

    * Java Method to calculate difference between two dates in Java

    * without using any third party library.

    */

    private static long daysBetween(Date one, Date two) {

    long difference = (one.getTime()-two.getTime())/86400000;

    return Math.abs(difference);

    }

    /*

    * Java Method to find number of days between two dates

    * in Java using JodaTime library. To find difference

    * we first need to convert java.util.Date to LocalDate

    * in JodaTime.

    */

    public static int daysBetweenUsingJoda(Date d1, Date d2){

    return Days.daysBetween(

    new LocalDate(d1.getTime()),

    new LocalDate(d2.getTime())).getDays();

    }

    }

    Output:

    Please enter two dates in format yyyy/MM/dd to compare

    2014/11/23

    2014/11/25

    Number of days between date 2014/11/23 and 2014/11/25 is : 2

    difference between two dates 2014/11/23 and 2014/11/25 is : 2

    You can see that number of days between two dates are correct and output of both our own method and joda-time is same.

    展开全文
  • If you want literal 24 hour days, (a duration), you can use the Duration class instead: LocalDate today = LocalDate.now() LocalDate yesterday = today.minusDays(1); // Duration oneDay = Duration....

    I know there are lots of questions on SO about how to get, but I want and example using new Java 8 Date api. I also know JodaTime library, but I want a work way without external libraries.

    Function needs to complain with these restrictions:

    Prevent errors from date savetime

    Input are two Date's objects (without time, I know localdatetime, but I need to do with date instances)

    解决方案

    If you want logical calendar days, use DAYS.between() method from java.time.temporal.ChronoUnit:

    LocalDate dateBefore;

    LocalDate dateAfter;

    long daysBetween = DAYS.between(dateBefore, dateAfter);

    If you want literal 24 hour days, (a duration), you can use the Duration class instead:

    LocalDate today = LocalDate.now()

    LocalDate yesterday = today.minusDays(1);

    // Duration oneDay = Duration.between(today, yesterday); // throws an exception

    Duration.between(today.atStartOfDay(), yesterday.atStartOfDay()).toDays() // another option

    For more information, refer to this document (and other documents re:Java 8 from Oracle).

    展开全文
  • python时间计算天数

    2021-07-08 17:44:58
    now_td = datetime.date.today() month = now_td.month + 4 day = now_td.day year = now_td.year # 直接多原时间进行替换,得到的任然是时间对象 future_td = now_td.replace(year=year, month=month, day=day) # ...
  • excel表格怎么计算两个日期之间的天数?以计算距国庆日期为例:见图一1、在F5单元格输入公式:E5-D52、在F5单元格单击右键,在菜单中选择:设置单元格格式...这时会跳出单元格设置对话框见图二在对话框中选择:数值...
  • (请写出计算过程)解:增加的部分就是原来的:3/5+10%所以原来要做:280/(3/5+10%)=400件(2)某校办工厂这个月生产本子的增值额为3万元.如果按增值额的17%交纳增值税,这个月应交纳增值税多少元?(请写出计...
  • 如何在Excel中得到两个日期相减的天数如何在Excel中得到两个日期相减的天数 有的小伙伴在使用Excel软件时,想要知道两个日期相减后的天数,但是却不知道使用什么公式,也不知道公式中的数据的含义,那么小编就来为大家...
  • 今天早上突然要用到一个计算某月有多少天的函数,翻了半天没找到系统自带着中函数,于是动手写了一个,如下:**返回某个月有多少天FUNCTION HowMonthDay()PARAMETERS cDateLOCAL Days as Integer ,Years as Integer...
  • Excel函数之~计算日期、天数、星期

    千次阅读 2020-05-24 20:55:43
    计算从开始日期算起的数个月之前或之后的日期 1.2 官方说明: 计算从开始日期算起的数个月之前或之后的日期.返回值为1(1900年1月1日)~2958465(9999年12月31日)范围之间的整数(序列号值). 1.3 解赋大白话: 计算在...
  • 在Excel中,Eomonth函数,可以计算出N个月后的月份最后一天最后一天的日期。 参数含义: Start_date:表示某个具体的日期,如2021/4/12 Month:是Start_date之前/之后的月数 Eomonth(today(),0),返回本月的...
  • excel相差天数计算

    2021-06-25 09:20:19
    excel表格中如何计算两个日期之间的天数?我想计算两个日期之间的天数, 例如...首先在B2单元格输入公式=TODAY()-A2,TODAY()是指今天的日期,会随时间的改变而改变,按enter键则可得出这两个时间相差的天数了;把鼠...
  • 日期计算器输入天数计算日期Would you like to know how many days old are you today? Can you tell what will be the date 78 days from now? How many days are left till Christmas? How many days have passed ...
  • vue 过滤器 计算剩余天数

    千次阅读 2018-12-03 14:48:40
    // 计算天数 Vue.filter('getDay',function (end_time,start_time) { if(start_time==0){ var today=new Date(); //当前时间 start_time = Math.ceil(today.getTime()/1000); } var day = Math...
  • dataframe中有两列数据'begin_date'和'end_date',现在通过这两列计算新的一列'work_days'。'begin_date'和'end_date'的数据格式均为:Timestamp需求:1.如果'end_date'为空,则用当前日期 - 'begin_date';2.如果''...
  • 我的某个IOS项目中,需要分组来显示签到记录, 每天可能有多次签到, 用天来对签到记录进行分组。每一条签到记录都有一个时间字段,分组的标题就是用时间中的日期来显示的,例如...于是我写了一个函数计算某个时间...
  • 如何利用python自带的库玩转时间 ...用datetime可以自己制作一个天数计算器,计算从某年某月某日到现在,或者到任何一天,之间的天数。 先放一张图片,比如计算恋爱天数 代码如下 用combobox下拉...
  • 如果您想要逻辑日历日 ,请使用DAYS.between()方法:LocalDate dateBefore; LocalDate dateAfter;...如果你想要24小时的文字 ( 持续时间 ),你可以使用Duration类:LocalDate today = LocalDate.now()...
  • /bin/bash#the days between your input and today!#2012-10-07year(){while read -p "please input the year(must be YYYY ): " YEARdoif ((YEAR>=1990)) && ((YEAR<=9999));thenecho $YEARbreakfi.....
  • – Beginvar today = new Date();var now = today.getDate();var year = today.getYear();if (year < 2000) year += 1900; // Y2K fixvar month = today.getMonth(); var monarr = new Array(31, 28, 31, 30, 31,...
  • 2、求出两个日期之间相差的毫秒数,除以一天的毫秒数即得到两个日期之间相差的天数。 js获取今天的格式化的日期 //获取格式化的日期 function today(){ var today=new Date(); var str=""; str+=today....
  • 显示当前月的日历,如何计算某月份的天数以及一个给定日期相应的是星期几。 思路: 1.构造一个日历对象,并用当前的日期和时间初始化。 2.将date设置为这个月的第一天,并得到这一天是星期几。 3.变量...
  • 科特林 这是计算从今天到某个日期的天数的示例: val millionSeconds = yourDate.time - Calendar.getInstance().timeInMillis leftDays.text = TimeUnit.MILLISECONDS.toDays(millionSeconds).toString() +"days" ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 8,770
精华内容 3,508
关键字:

today计算天数

友情链接: abc.zip