Rectangle 27 5

Adding/subtracting number of days to/from date in pure XSLT 1.0:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="givenDate" select="'2014-05-13T00:00:00'"/>
<xsl:param name="daysDiff" select="-3"/>

<xsl:variable name="JDN">
    <xsl:call-template name="JDN">
        <xsl:with-param name="date" select="$givenDate" />
    </xsl:call-template>
</xsl:variable>

<xsl:variable name="newDate">
    <xsl:call-template name="GD">
        <xsl:with-param name="JDN" select="$JDN + $daysDiff" />
    </xsl:call-template>
</xsl:variable>


<xsl:template match="/">
    <output>
        <GivenDate><xsl:value-of select="$givenDate"/></GivenDate>
        <NewDate><xsl:value-of select="$newDate"/></NewDate>
    </output>
</xsl:template> 


<xsl:template name="JDN">
    <xsl:param name="date"/>
    <xsl:param name="year" select="substring($date, 1, 4)"/>
    <xsl:param name="month" select="substring($date, 6, 2)"/>
    <xsl:param name="day" select="substring($date, 9, 2)"/>
    <xsl:param name="a" select="floor((14 - $month) div 12)"/>
    <xsl:param name="y" select="$year + 4800 - $a"/>
    <xsl:param name="m" select="$month + 12*$a - 3"/>
    <xsl:value-of select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
</xsl:template> 

<xsl:template name="GD">
    <xsl:param name="JDN"/>
    <xsl:param name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
    <xsl:param name="e" select="4*$f + 3"/>
    <xsl:param name="g" select="floor(($e mod 1461) div 4)"/>
    <xsl:param name="h" select="5*$g + 2"/>
    <xsl:param name="D" select="floor(($h mod 153) div 5 ) + 1"/>
    <xsl:param name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
    <xsl:param name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
    <xsl:param name="MM" select="substring(100 + $M, 2)"/>
    <xsl:param name="DD" select="substring(100 + $D, 2)"/>
    <xsl:value-of select="concat($Y, '-', $MM, '-', $DD)" />
</xsl:template>     

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<output>
   <GivenDate>2014-05-13T00:00:00</GivenDate>
   <NewDate>2014-05-10</NewDate>
</output>

-- Note that the givenDate's parameter value is a string and as such must be wrapped in single quotes.

xml - Date operations on xsl 1.0 - Stack Overflow

xml datetime xslt-1.0 operation
Rectangle 27 2

Is the date value a string or an integer?

Anyway, supposing that date is an int, you could try this:

function my_sort($val1, $val2) {
    $compare_id = $val1['Member']['id'] - $val2['Member']['id'];
    if($compare_id == 0) {
        return $val1['Payment']['date'] - $val2['Payment']['date'];
    }
    else return $compare_id;
}

and then call:

usort($array, 'my_sort');

PHP sort multi dimensional array - Stack Overflow

php arrays sorting multidimensional-array
Rectangle 27 7

The format "yyyy-mm-dd" is an ISO standard and when encountering it, CDate() expects the "yyyy" part to be followed by "mm-dd", never by "dd-mm". So a date string in that format is unambiguous; it represents the same date value regardless of the user's locale. And CDate() will comply.

Furthermore, there is no way to give CDate() a date string formatted as "yyyy-dd-mm" and get the date value you intend back. So CDate("2011-02-01") will always give you the date value #Feb 1 2011# (regardless of your locale), even if you intended that string to represent #Jan 2 2011#. And CDate("2011-31-01") will throw a type mismatch error.

Also note that this only works for dates after the year 100. (See Heinzi's comment.)

Note that this only works for dates after the year 100: CDate("0001-02-03") yields the 1st Feb. 2003.

vba - Parse data and time string to Access Date value - Stack Overflow

ms-access vba ms-access-2007
Rectangle 27 2

Assuming you are using SQL*Plus (or SQL Developer) the default NLS_DATE_FORMAT is applied when a DATE value is displayed. To verify your current format you can run:

select value 
from nls_session_parameters 
where parameter = 'NLS_DATE_FORMAT';
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';

Then you should see the time as well.

Convert string to date in Oracle SQL - Stack Overflow

sql oracle to-date
Rectangle 27 2

Assuming you are using SQL*Plus (or SQL Developer) the default NLS_DATE_FORMAT is applied when a DATE value is displayed. To verify your current format you can run:

select value 
from nls_session_parameters 
where parameter = 'NLS_DATE_FORMAT';
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';

Then you should see the time as well.

Convert string to date in Oracle SQL - Stack Overflow

sql oracle to-date
Rectangle 27 2

I agree with some of the other posters here that you probably are better off keeping it as a date value and only displaying the year. For calculation purposes, maybe you can set all of the date values in that column to 01-01-YYYY. Then when you want to display that year in your view, do:

#- my_date_value=Wed, 18 Sep 2013 19:21:11 UTC +00:00
my_date_value.year
=> 2013
<%= my_date_value.year %>

However, if you are determined to make this a date value, then you can do a migration like the following to convert it to a year-only integer field:

class ChangeThingDateToYear < ActiveRecord::Migration
  def change
    add_column :things, :my_year_value, :integer

    # You need to tell ActiveRecord to refresh the object
    MyDatedThings.reset_column_information
    MyDatedThings.all.each do |r|
       r.my_year_value = r.my_date_value.year
       r.save!
    end

    remove_column :things, :my_date_value

    # If you want to keep the same column name
    rename_column :things, :my_year_value, :my_date_value
  end
end

Thanks for giving your point of view and the code for the migration. Very useful! I'm actually using a date type and working with the year part only as you said, so I'm going to try the other approach now. Thanks.

Rails migration: change column data type from date to integer - Stack ...

ruby-on-rails migration
Rectangle 27 3

Starting with your date:

<xsl:variable name="date" select="'2013-09-13T00:40:00'" />

Then, apparently, you can use:

<xsl:sequence select="$date +27*xs:dayTimeDuration('P1D')"/>

But this only works in XSLT 2.0, which I don't have.

You can check what day of the week it is by using:

<xsl:variable name="day-of-week">
  <xsl:call-template name="calculate-day-of-the-week"> <!--0-6, where 0=Sun,1=Mon,2=Tue,3=Wed,4=Thu,5=Fri,6=Sat-->
      <xsl:with-param name="date-time" select="$date" />
  </xsl:call-template>
</xsl:variable>

<xsl:template name="calculate-day-of-the-week">
  <xsl:param name="date-time"/>
  <xsl:param name="date" select="substring-before($date-time,'T')"/>
  <xsl:param name="year" select="substring-before($date,'-')"/>
  <xsl:param name="month" select="substring-before(substring-after($date,'-'),'-')"/>
  <xsl:param name="day" select="substring-after(substring-after($date,'-'),'-')"/>
  <xsl:variable name="a" select="floor((14 - $month) div 12)"/>
  <xsl:variable name="y" select="$year - $a"/>
  <xsl:variable name="m" select="$month + 12 * $a - 2"/>
  <xsl:value-of select="($day + $y + floor($y div 4) - floor($y div 100) + floor($y div 400) + floor((31 * $m) div 12)) mod 7"/>
</xsl:template>

'day-of-week' will give you a value of 0-6 (Sun to Sat). This will account for leap-years and will work in XSLT 1.0

<xsl:if test="day-of-week=0>
  <xsl:sequence select="$date +28*xs:dayTimeDuration('P1D')"/>
</xsl:if>
<xsl:if test="day-of-week=6>
  <xsl:sequence select="$date +29*xs:dayTimeDuration('P1D')"/>
</xsl:if>

I can't test this code without XSLT 2.0, but hopefully this will get you going in the general direction.

+1, good answer. BTW if you want XSLT 2.0, you can download Saxon free from sourceforge.net/projects/saxon

I am going to try this out tomorrow and see how it goes. Will come back with my result. Thanks for the help!

xml - How to check if a date falls on a weekend in an XSLT file - Stac...

xml xslt xslt-1.0 xslt-2.0
Rectangle 27 6

It's sorting based on alphanumeric (ASCII) values, not based on date-values (It will also put November before October). You need jquery to interpret each cell as a date value and then sort them accordingly. If their built-in type detection isn't working, you have to force the data type. See http://www.terminally-incoherent.com/blog/2008/09/29/jquery-tablesorter-list-of-builtin-parserssorters/ for more information.

Works great. I love the fact I have manual control over the columns. Thanks so much!

jquery - Tablesorter - Dates Problem - Stack Overflow

jquery tablesorter
Rectangle 27 86

That's simply the toString value of the prototype object of the Date constructor function.

Date.prototype.toString(); // "Invalid Date"

You can override it if you like...

Date.prototype.toString = function() { return "I like turtles." };

var myDate = new Date(1331869050000);
myDate.__proto__; // I like turtles.

A little off topic, but __proto__ is in the current working draft for the next version of ECMAScript, codename Harmony.

__proto__

You are close, but the "why" part is still missing, the reason is that the Date.prototype object is a Date object by itself but its [[PrimitiveValue]] internal property is NaN (this internal property on date objects stores the time value), so it doesn't represent actually a valid date. Date.prototype is an object similar to new Date(NaN)...

so basically, there is no error and Chrome is just reflecting the toString() which doesn't show the true value

Plus one for the love of turtles

javascript - Why does Chrome Dev Tool show a dates __proto__ as Invali...

javascript google-chrome-devtools
Rectangle 27 3

Looks like you're using the wrong MySQL functions here. You're looking for STR_TO_DATE() which will convert that date string into a date value which MySQL can work with. You can then use DATE_FORMAT() to convert it to a new date string of your choosing.

SELECT DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y'), '%Y/%m/%d') AS t FROM tracker;

mysql - Changing date format using FORMAT - Stack Overflow

mysql sql date date-format
Rectangle 27 1

setTime() sets the the time regardless of the fact that a date contained no time value (00:00:00) or no date value (01.01.1970).

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd-yyyy hh:mm zzz");
cal.setTime(sdfDate.parse(DATE+ " " + TIME));

Should work out for you.

Java Calendar object, setting date and time separately - Stack Overflo...

java date calendar simpledateformat
Rectangle 27 62

The Date constructor accepts any value. If the primitive [[value]] of the argument is number, then the Date that is created has that value. If primitive [[value]] is String, then the specification only guarantees that the Date constructor and the parse method are capable of parsing the result of Date.prototype.toString and Date.prototype.toUTCString()

setFullYear
setTime

ECMA-262 r3 does not define any date formats. Passing string values to the Date constructor or Date.parse has implementation-dependent outcome. It is best avoided.

YYYY-MM-DD
Date
/**Parses string formatted as YYYY-MM-DD to a Date object.
 * If the supplied string does not match the format, an 
 * invalid Date (value NaN) is returned.
 * @param {string} dateStringInRange format YYYY-MM-DD, with year in
 * range of 0000-9999, inclusive.
 * @return {Date} Date object representing the string.
 */

  function parseISO8601(dateStringInRange) {
    var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
        date = new Date(NaN), month,
        parts = isoExp.exec(dateStringInRange);

    if(parts) {
      month = +parts[2];
      date.setFullYear(parts[1], month - 1, parts[3]);
      if(month != date.getMonth() + 1) {
        date.setTime(NaN);
      }
    }
    return date;
  }

Thanks Garrett. That function you provided in the link is the best far more concise than anything else I've been able to find, and it works with the date format that I use through the other 99% of the app! Much more consistent than what I was using.

This is, alas, not good, since many structured formats just use W3C date/time formats (ISO-8601 with full specs or so). So while timestamps are better in many ways (simpler, more efficient, work with all browsers), it is often required that standard date/time data is parser from javascript. I wonder if jQuery or such would have better parsing methods.

thanks for pointing out, going crazy why chrome works fine parsing dates while ie7 is saying NAN, good thing $.datepicker.parseDate from jquery is able to parse dates

What if you want the miliseconds as well?

I've been using a function that was derived from this code in in order to parse an ISO date/time from a string. On rare occasions I will have a seemingly valid value enter into the if (month != date.getMonth() + 1) statement, invalidating the date. For example, 2014-06-01T01:09:22.68. Can you explain the purpose of this if statement?

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

javascript - Date constructor returns NaN in IE, but works in Firefox ...

javascript internet-explorer date
Rectangle 27 5

How about something like this? It will test if it is a Date object or a date string:

function = isDate(value) {
  var dateFormat;
  if (toString.call(value) === '[object Date]') {
    return true;
  }
  if (typeof value.replace === 'function') {
    value.replace(/^\s+|\s+$/gm, '');
  }
  dateFormat = /(^\d{1,4}[\.|\\/|-]\d{1,2}[\.|\\/|-]\d{1,4})(\s*(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d\s*[ap]m)?$/;
  return dateFormat.test(value);
}

I should mention that this doesn't test for ISO formatted strings but with a little more work to the RegExp you should be good.

javascript - Check if a string is a date value - Stack Overflow

javascript date-format
Rectangle 27 5

How about something like this? It will test if it is a Date object or a date string:

function = isDate(value) {
  var dateFormat;
  if (toString.call(value) === '[object Date]') {
    return true;
  }
  if (typeof value.replace === 'function') {
    value.replace(/^\s+|\s+$/gm, '');
  }
  dateFormat = /(^\d{1,4}[\.|\\/|-]\d{1,2}[\.|\\/|-]\d{1,4})(\s*(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d\s*[ap]m)?$/;
  return dateFormat.test(value);
}

I should mention that this doesn't test for ISO formatted strings but with a little more work to the RegExp you should be good.

javascript - Check if a string is a date value - Stack Overflow

javascript date-format
Rectangle 27 2

Use TRUNC to truncate the date to the start of the month and GROUP BY/ORDER BY that date value and just covert it to a string in the SELECT clause:

SELECT   TO_CHAR( TRUNC( DATEIN, 'MM' ), 'MON-YYYY') AS MY 
FROM     HRM_ATTENDANCE 
GROUP BY TRUNC( DATEIN, 'MM' ) 
ORDER BY TRUNC( DATEIN, 'MM' )

However, since your data appears to only be for the first of the month then this can be simplified to:

SELECT   TO_CHAR( DATEIN, 'MON-YYYY') AS MY 
FROM     HRM_ATTENDANCE 
GROUP BY DATEIN 
ORDER BY DATEIN

mysql - Oracle date to_char group and order by - Stack Overflow

mysql oracle
Rectangle 27 113

You haven't set the timezone only added a Z to the end of the date/time, so it will look like a GMT date/time but this doesn't change the value.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

If you have a date, say 2012-12-06T06:00:00 without the Z, does that represent GMT?

@binarygiant You would have to ask those who sent it. It could be the local time of the sender.

date - Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezon...

java date timezone date-format simpledateformat
Rectangle 27 113

You haven't set the timezone only added a Z to the end of the date/time, so it will look like a GMT date/time but this doesn't change the value.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

If you have a date, say 2012-12-06T06:00:00 without the Z, does that represent GMT?

@binarygiant You would have to ask those who sent it. It could be the local time of the sender.

date - Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezon...

java date timezone date-format simpledateformat
Rectangle 27 1

So the point here is 'actually use a range' and also be aware of the Date value you get in return. So as a basic example in principle.

The time right now as I execute this is:

var date = new Date()
ISODate("2015-07-17T04:02:04.471Z")

So if you presume then that your actual timestamp in the document is "not exactly to the minute" (like above) nor is it likely the "random record" is so then the first thing to do is "round" it to the minute:

date = new Date(date.valueOf() - date.valueOf() % ( 1000 * 60 ))
ISODate("2015-07-17T04:02:00Z")

And of course the "end date" is just one minute after that:

var endDate = new Date(date.valueOf() + ( 1000 * 60 ))
ISODate("2015-07-17T04:03:00Z")

Then when you query "rqndom" you just get the "range", with $gte and $lt operators:

db.random.find({ "postedAt": { "$gte": date, "$lt": endDate } })
  • Search on the "range" betweeen that value and the next minute

thx for the answer and will try this, I had same thing in mind but thought we might loose date object reference if we change the date values, do we loose that?

@noName Depends on what you do. You can always cast to a new variable with the "rounded" values from the source within your retrieved document, just as shown. And that is probably the safest thing to to. You can do other methods that manipulate the date value as retrieved, but I have not shown those here.

meteor - Date range queries mongodb - Stack Overflow

mongodb meteor mongodb-query
Rectangle 27 10

The value for fmt:formatDate is suppose to be a Date object (java.util.Date). If the task.startDate is a date as a String, then you need to convert it beforehand.

<fmt:parseDate value="${task.startDate}" pattern="yyyy-MM-dd HH:mm:ss" var="myDate"/>
<fmt:formatDate value="${myDate}" var="startFormat" pattern="yyyy-MM-dd"/>
java.util.Date

java - How to use format date as "yyyy-MM-dd" with JSTL? - Stack Overf...

java jsp date jstl
Rectangle 27 1

@MikeyByCrikey posted a good starting point, but there are a couple questions you need to answer before you can convert your string value to a date:

A. What date is represented by '0'? That is, where does this millisecond count value start from? In @Mikey's example the Unix 'beginning' date (also called the 'epoch date') of 01-Jan-1970 is used, but there isn't enough information in your post to know what the '0' date actually is. Other systems use different epoch dates. For example, on systems running the VMS operating system the epoch date is 17-Nov-1858 (happens to be Julian day 2,400,000); Windows (32 and 64 bit versions) uses 01-Jan-1601 (first year of the 400 year Gregorian calendar cycle which was current when this OS was designed); and other computer systems use different '0' dates.

B. Converting a string to a number in Oracle is fairly easy. I'd just use the TO_NUMBER function, as in:

strMillisecs   VARCHAR2(100) := '1230748200000';
  nMilliseconds  NUMBER;

  nMilliseconds := TO_NUMBER(strMillisecs);

C. Once you've gotten to this point (the '0' date is known, and the string is converted to a number) the year can be extracted as follows:

dtEpoch_date         DATE := TO_DATE('01-JAN-1970', 'DD-MON-YYYY');  -- for example
  strMillisecs         VARCHAR2(100) := '1230748200000';
  nMilliseconds        NUMBER;
  dtTarget_date        DATE;
  nYear                NUMBER;
  nMillisecs_in_a_day  NUMBER := 86400000;

  nMilliseconds := TO_NUMBER(strMillisecs);

  -- Note: there are 

  dtTarget_date := dtEpoch_date + (nMilliseconds / nMillisecs_in_a_day);

  -- FINALLY!

  nYear := TO_NUMBER(TO_CHAR(dtTarget_date, 'YYYY'));

Share and enjoy.

Thanks Bob for the answer..can you please have a look at the edit on question where i describe the latest issue i am coming

sql - Get Date from a long var char milliseconds value stored in oracl...

sql oracle date timestamp date-arithmetic