I have a database which keeps dates as a number like 1488950859, and when I run the software which associated to the DB, it shows this date : 2017-March 08 08:27 AM
another example is :
1395208154 = 2014 March 19, 8:49 AM
anyone can give me a hand and reveal this mysterious format?
I think you are storing date in a column of type nvarchar because when you save date in nvarchar column it converts into number.
I got it..
this is called Unix timestamp, it is no. of seconds since standard epoch of 1st Jan 1970.
to return it to ordinary date format , use below function
Public Function UnixToDateTime(ByVal strUnixTime As String) As DateTime
Dim nTimestamp As Double = strUnixTime
Dim nDateTime As System.DateTime = New System.DateTime(1970, 1, 1, 0, 0, 0, 0)
nDateTime = nDateTime.AddSeconds(nTimestamp)
Return nDateTime
End Function
Related
Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
I have tables that contain a lot of rows, each record is excellent by whom and on what date was added.
I want to group all records, based on AddedBy and AddedOn fields.
The problem is that the dates are in full including the miliseconds and I only want to group by day, month or year.
I hereby noted that at Compilation time I do not know which table it is and Therefore I use with System.Dynamic.LINQ library.
Below I demonstrate how did a grouping by datetime using System.Dynamic.LINQ library:
Dim groupColl= myColl.GroupBy("new(AddedName, AddedOn), it").Select("new(Key.AddedName, Key.AddedOn, Count() AS Count)")
But the problem is that I need to grouping by day, month or year.
In sql server I found how to do it, by day or by month or by year.
In Lambda Expression also found the way how to do it, but only by day.
But through System.Dynamic.LINQ not find any way how to do it.
Below I demonstrate how I'm doing this in sql server and Lambda Expression:
Using SQL SERVER:
SELECT AddedBy, DATEADD(DAY, DATEDIFF(DAY, 0, AddedOn), 0) as AddedOn, count(*)
FROM myTable
GROUP BY AddedBy, DATEADD(DAY, DATEDIFF(DAY, 0, AddedOn), 0)
Using Lambda Expression vb.net code
Dim groupColl = myCollection.GroupBy(Function(x) New With {Key x.AddedBy, Key .AddedeOn_Day = DbFunctions.TruncateTime(x.AddedOn)}).Select(Function(y) New With {y.Key.AddedBy, y.Key.AddedeOn_Day, y.Count})
I would be grateful to anyone who would help me how to do it using System.Dynamic.LINQ Library
Thanks
I found a solution!
My solution basis found here:
Using DateDiff with Linq.Dynamic library for fetching today records
Using Lambda Expression
Dim groupColl = myColl.GroupBy(Function(x) New With {Key x.AddedBy, Key .AddedeOn_Month = DbFunctions.CreateDateTime(x.AddedOn.Year, x.AddedOn.Year, x.AddedOn.Month, 1, 0, 0.0)}).Select(Function(y) New With {y.Key.AddedBy, y.Key.AddedeOn_Month , y.Count})
I added the following lines to the System.Dynamic.Linq recognize the DbFunctions:
Dim type = GetType(DynamicQueryable).Assembly.[GetType]("System.Linq.Dynamic.ExpressionParser")
Dim field = type.GetField("predefinedTypes", BindingFlags.[Static] Or BindingFlags.NonPublic)
Dim predefinedTypes = DirectCast(field.GetValue(Nothing), Type())
Array.Resize(predefinedTypes, predefinedTypes.Length + 1)
predefinedTypes(predefinedTypes.Length - 1) = GetType(DbFunctions)
field.SetValue(Nothing, predefinedTypes)
Then I added:
Dim groupColl = myColl.GroupBy("new(AddedName, DbFunctions.CreateDateTime(AddedOn.Year, AddedOn.Month, 1, 0, 0, 0.0) AS AddedOn_Month), it").Select("new(Key.AddedName, Key.AddedOn_Month, Count() AS Count)")
Now it works great.
Hi i am pulling the date from sql server which returns this: 12/19/2014 4:17:31 PM
However I only want it to return 12/19/2014
I am using this to get the order date: txtOrderDate.Text = dt.Rows(0).Item("OrderDate")
How do I convert this to only return the date in this format MM/dd/YYYY?
I cannot do it using the SQL Statement because I am pulling other columns as well.
Thanks
You could get the output desidered with
Dim dt as DateTime
if Not dt.Rows(0).IsNull("OrderDate") _
AndAlso DateTime.TryParse(dt.Rows(0).Item("OrderDate"), _
CultureInfo.InvariantCulture, _
DateTimeStyles.None, dt) Then
txtOrderDate.Text = dt.ToString("MM/dd/yyyy")
else
.... ' Something to do in case of null or invalid date
End If
The use of DataRow.IsNull and DateTime.TryParse is a precautionary step to avoid any possible exception in case your column "OrderDate" is null or not in a correct format.
I think this will work:
txtOrderDate.Text = dt.Rows(0).Item("OrderDate").ToString("MM/dd/yyyy")
However, it's been a while since I've had to work with DataTable cells directly; there's a chance the above code will only have the Object type, rather than the VB.Net DateTime type, and therefore that .ToString() overload won't be available. If that's the case, do this instead:
txtOrderDate.Text = DirectCast(dt.Rows(0).Item("OrderDate"), DateTime).ToString("MM/dd/yyyy")
In a school website, I want to enable the admin to filter students based on date range when they were born. Dates in my tblStudent are stored as strings, so I cannot use:
SELECT ts.Name from tblStudent ts WHERE ts.BirthDay>'1367/01/31' AND ts.BirthDay<'1377/01/31'
I have saved dates (Jalali Format) in database table tblStudent. I need to do comparison based on dates. So I need to convert date strings to date type in sql server. To this purpose I used:
SELECT convert(date,tblStudent.BirthDay) from tblStudent
However,It stops after 27 results with the following error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I have the following date strings in my tblStudent table.
1379/09/01
1375/04/20
1378/03/02
1378/03/21
1378/04/18
1378/04/18
1378/05/05
1375/04/20
1379/01/03
1378/03/01
1370/09/09
1378/03/22
1375/09/15
1379/09/01
1379/09/10
1375/04/08
1375/05/06
1370/09/09
1379/10/10
1375/04/10
1375/11/01
1375/04/04
1375/08/11
1375/05/05
1376/09/19
1375/12/12
1376/01/13
1375/15/10
1375/04/14
1375/04/04
1375/05/14
1374/11/11
1375/05/30
1375/05/14
1377/12/13
1377/02/31
1377/12/14
1377/01/13
1375/05/31
1377/11/05
1377/07/05
1375/05/31
1377/03/01
1377/04/01
1377/05/02
1377/05/04
1377/03/03
1377/01/14
1377/05/30
1377/04/31
1375/05/30
1376/06/12
1375/12/10
1377/08/14
1377/03/04
1375/04/08
1375/07/18
1375/08/09
1375/09/12
1375/11/12
1376/12/12
1375/01/02
1375/05/09
1375/04/09
1376/01/01
1375/01/30
1377/04/04
1375/05/23
1375/05/01
1377/02/01
1367/12/05
1375/05/31
1373/03/29
1373/03/03
1375/05/05
Is there a way to convert these string dates to date type and then compare them with some query? For example, such a query can be:
SELECT ts.Name from tblStudent ts where ts.BirthDay>'1375/05/31'
I think you can make them ints and compare them:
SELECT ts.Name
FROM tblStudent ts
WHERE CONVERT(INT,REPLACE(ts.BirthDay,'/','') > 13670131
AND CONVERT(INT,REPLACE(ts.BirthDay,'/','') < 13770131
Or for your second example:
SELECT ts.Name
FROM tblStudent ts
WHERE CONVERT(INT,REPLACE(ts.BirthDay,'/','') > 13750531
This would work because having the order Year-Month-Day will ensure that the int representation of a later time will be greater than the int representation of an earlier time.
I really do not know if this is the best idea, but it is an idea of how to do it. After all you would be using a conversion.
From C# you have a few options:
If your input is string:
var dateInt = Int32.Parse(dateString.Replace("/",""));
If your input is Date then:
var dateInt = Int32.Parse(dateValue.ToString("yyyyMMdd"));
You could also pass the string itself in the db and let the db do the work for you :
DECLARE #Date AS VARCHAR(10)
SET #Date = ...--This will be filled with the inputed string
DECLARE #DateINT AS INT
SET #DateINT = CONVERT(INT,REPLACE(#Date,"/",""))
I am trying to create a database that called rawData. The db will hava a column for the id, a foreign user id (_id from another table), data and finally a timestamp.
My question is how can I create a timestamp in SQlite and store it in the db also what type should the column be, text? the database needs to be able to store 150 float values a second and time stamp each of those 150 entries. Additionally since SQlite doesn't have a float type should i use real as the column type?
public class RawDatabase{
public static final String TABLE_RAW_DATA = "rawData";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FOREIGN_USER_ID = "foreignUserId";
public static final String COLUMN_DATA = "data";
public static final String COLUMN_TIME_STAMP = "timeStamp";
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_RAW_DATA + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_FOREIGN_USER_ID
+ " integer, " + COLUMN_DATA
+ " real, " + COLUMN_TIME_STAMP
+ " text not null);";
}
The documentation says:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
If you need only seconds precision, use integers in Unix Time format.
Otherwise, use floating-pointer numbers for fractional seconds.
This is a running example of DAO with SQlite and Date in Java:
First create column MY_DATE TIMESTAMP in your TABLE and populate like this:
PreparedStatement st = connection.prepareStatement("INSERT INTO ......");
st.setDate(1, new java.sql.Date(lettura.getData().getTime()));
And to retrieve data first i get date in String type:
String dateStr = rs.getString(1);
Date myDate = new java.util.Date(Long.parseLong(dateStr));