Date issue while extracting from mysql in asp.net - asp.net

I have a date column in my mysql db which stores
1999-03-30
as the date.
Using webservices when i extract the value by following code
DOB = Convert.ToDateTime(reader["DOB"]),
The output on the screen is
/Date(922732200000)/
How to get 1999-03-30 and what is this value /Date(922732200000)/...

The easiest way to handle dates in mysql is to use UNIX TIMESTAMP to store and retrieve dates.
For example:
The query for inserting date should be:
string query="insert into myTable(name,date) values ("StackOverflow",unix_timestamp('yyyy-mm-dd HH:mm:ss')";
eg. If you want to enter the current datetime, this query can be re-Written as:
string query="insert into myTable(name,date) values ("StackOverflow",unix_timestamp(current_timestamp)";
and when you retrive the data from your table, the query should be:
string queryToretrivedata="select name, from_unixtime(date) as DOB from myTable";
While the MySqlreader reads the data, the statement in your function should be:
DateTime DOB= Convert.ToDateTime(reader["DOB"].ToString());
Also, if you want to convert this dateTime into string you can use
string DateInString==DOB.ToString("yyyy-MM-dd HH:mm:ss"); //For 24 hour Format
string DateInString==DOB.ToString("yyyy-MM-dd hh:mm:ss tt"); //For 12 Hour format

Related

How to use a column from a Hive table to define the timezone in from_utc_timestamp function

I have a hive table, with a transaction utc_time_date - in UTC, and timezone stored as a string
customerid string
sessionid string
utc_time_date string
defined_cst timestamp
variable_time timestamp
store_nbr int
utc_date string
defined_cst_visit_date string
variable_date string
tc string
transactiontotal double
usegiftcardsfirst int
associate_flag int
new_user int
time_zone string
utc_time_date was a column called session in a subquery.
When I use FROM_UTC_TIMESTAMP(session, 'CST'), I get the correct time/date combination.
Here is a snippet of the code that created these columns
session as UTC_Time_Date, FROM_UTC_TIMESTAMP(session, 'CST') as Defined_CST,
FROM_UTC_TIMESTAMP(session, time_zone) as Variable_time,
storeid as store_nbr, transactiondate as UTC_Date,
to_date(FROM_UTC_TIMESTAMP(session, 'CST')) as defined_cst_visit_date,
to_date(FROM_UTC_TIMESTAMP(session, time_zone)) as variable_date,
tc, transactionTotal, useGiftCardsFirst, associate_flag, new_user, time_zone
But - FROM_UTC_TIMESTAMP(session,timezone) returns incorrect information as follows
utc_time_date defined_cst variable_time variable_date
11/6/2016 1:28 11/5/2016 20:28 11/6/2016 1:28 11/6/2016
Can one use a column as input for the timezone in this function? It's clear you can use a timestamp column as input for the date field, as that works.
So, I figured out what caused my issue - when I created the timezone table, there was extraneous white space at the end of each line. This caused the function to error, and not return the correct timezone. Lesson learned - make sue I use sed to remove white space in .csv files before using them to create tables in Hive.

What data type should be used for timestamp in DynamoDB?

I am new to DynamoDB. I wish to create a table which using DeviceID as the hash key, Timestamp as my range key and some data.
{ DeviceID: 123, Timestamp: "2016-11-11T17:21:07.5272333Z", X: 12, Y: 35 }
In SQL, we can use datetime type for Timestamp, but in DynamoDB there is none.
What data type should I use? String? Number?
For the chosen data type, what kind of timestamp format should I write in? ISO format (e.g: 2016-11-11T17:21:07.5272333Z) or epoch time (e.g: 1478943038816)?
I need to search through the table through a range of time, e.g: 1/1/2015 10:00:00am until 31/12/2016 11:00:00pm
The String data type should be used for Date or Timestamp.
You can use the String data type to represent a date or a timestamp.
One way to do this is by using ISO 8601 strings, as shown in these
examples:
2016-02-15
2015-12-21T17:42:34Z
20150311T122706Z
DynamoDB Data type for Date or Timestamp
Yes, the Range queries are supported when the date is stored as String. The BETWEEN can be used on FilterExpresssion. I have got the items in the result using the below filter expressions.
FilterExpression without time:-
FilterExpression : 'createdate between :val1 and :val2',
ExpressionAttributeValues : {
':hkey' : year_val,
':rkey' : title,
":val1" : "2010-01-01",
":val2" : "2010-12-31"
}
FilterExpression with time:-
FilterExpression : 'createdate between :val1 and :val2',
ExpressionAttributeValues : {
':hkey' : year_val,
':rkey' : title,
":val1" : "2010-01-01T00:00:00",
":val2" : "2010-12-31T00:00:00"
}
Database Values:-
Format 1 - with timezone:
{"Item":{"createdate":{"S":"2010-12-21T17:42:34+00:00"},"title":{"S":"The Big New Movie 2010"},"yearkey":{"N":"2010"},"info":{"M":{"rating":{"N":"0"},"plot":{"S":"Nothing happens at all."}}}}}
Format 2 - without timezone:-
{"Item":{"createdate":{"S":"2010-12-21T17:42:34Z"},"title":{"S":"The Big New Movie 2010"},"yearkey":{"N":"2010"},"info":{"M":{"rating":{"N":"0"},"plot":{"S":"Nothing happens at all."}}}}}
Data type depends on your requirements.
You may use String using ISO format or Number using epoch format.
The advantage of ISO format (String) is human readability however DynamoDB does not support Time To Live (TTL) for this format. All filters work such as 'between' and 'range' as explained by notionquest.
Time To Live (TTL) for DynamoDB allows you to define when items in a table expire so that they can be automatically deleted from the database.
The advantage of using epoch format (Number) is that you can use the TTL feature and all filters.
TLDR;
Epoch format (Number type) - Can use Time To Live
ISO format (String type) - Cannot use Time To Live but is more human readable
for me to be able to filter out results when sending a query request, I used epoch format for DateTime, it is more efficient than using string.
imagine these scenarios: last 31 days, last 24 hours, ... again all is possible using the string format since it also has begins_with operator(please check 3rd example in below link on AWS doc) but numeric values are much more efficient in terms of performance while sorting(comparing) & calculation.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.KeyConditionExpressions
it is easy to convert date-time to epoch format
Javascript:
var date = new Date();
var epoch = date.getTime();
// converting back to date-time
var initial_date = new Date(epoch);
C#
var date = DateTime.UtcNow;
var epoch = new DateTimeOffset(date).ToUnixTimeSeconds();
// converting back to date-time
var initial_date = DateTimeOffset.FromUnixTimeSeconds(epoch);
Python
import time
epoch = time.time()
# converting back to date-time
initial_date = time.gmtime(epoch )
The Number data type OR the String data type
can be used for Date or Timestamp - not just String as the Accepted Answer on this Question incorrectly singles out while ignoring Number.
You can use the number data type to represent a date or a timestamp. One way to do this is by using epoch timeā€”the number of seconds since 00:00:00 UTC on 1 January 1970. For example, the epoch time 1437136300 represents 12:31:40 PM UTC on 17 July 2015.
For more information, see http://en.wikipedia.org/wiki/Unix_time.
...
You can use the String data type to represent a date or a timestamp. One way to do this is by using ISO 8601 strings, as shown in these examples:
2016-02-15
2015-12-21T17:42:34Z
20150311T122706Z
For more information, see http://en.wikipedia.org/wiki/ISO_8601.
DynamoDB Data type for Date or Timestamp

How can I remove the Time from a DateTime value?

In my SQL database, I have a column formatted as DateTime and when I retrieve data from that column in ASP.NET, I catch it on the Date variable, than pass the value to textbox:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.ToString
but when I debug my website, the txtSchedDate.Text still gives me the full DateTime value:
7/17/2013 12:00:00 AM
is it possible to eliminate the time value here and just return the date?
Have you tried using something like
txtSchedDate.Text = Y.Date.ToString("MM/dd/yyyy")
or which ever format you wish to display.
Have a look at
DateTime.ToString Method (String)
Converts the value of the current DateTime object to its equivalent
string representation using the specified format.
Custom Date and Time Format Strings
Standard Date and Time Format Strings
Convert.ToDateTime(dt.Rows(0)("SCH_DATE")).ToString("M/d/yyy")
you can get date by txtSchedDate.Text = Y.Date.ToShortDateString()
Besides answers above, you can try converting it in SQL server
SELECT CONVERT(varchar(15), GETDATE(), 11)
Keep in mind after converting it's VARCHAR(15) instead of DATETIME.
Once you have a Date object, you can get the constituent pieces if you wish as well, like this:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.Year & "-" & Y.Date.Month & "-" & Y.Date.Day
Or you can use the custom and standard date and time format strings mentioned by others.

VB.NET convert date string ddMMyyhhmmss to Date Object

I have a string that holds a value in the format of ddMMyyhhmmss.
Example 240512024707
I need to be able to convert this date to a real .NET Date object.
I am currently using CDate but it seems CDate does not recognize the format, is there any way of specifying the string format to CDate ???
row.Item("NoteDate") = CDate(n.noteText.Substring(0, 12).ToString).ToString("dd/MM/yyyy hh:mm:ss")
You have the string with the date, you know the exact format string - use DateTime.ParseExact (or DateTime.TryParseExact if you wish to avoid the potential exception being thrown):
DateTime.ParseExact("240512024707", "ddMMyyhhmmss", CultureInfo.InvariantCulture)

JavaME Date String to Long

Im using SQLite and trying to get my date into a blackberry datetime field.
in the DB its stored as string in format:
yyyy-MM-dd HH:mm
e.g. : 2012-02-01 15:45
But the field wants it as a Long.
date - The initial date value for this field. This is the number of milliseconds since midnight on January 1, 1970. To create an empty date field, set this parameter to Long.MIN_VALUE. This method will then remove the date value from this field, setting it to null.
And then convert it back again.
Check following code:
// conversion - string to long
long dateLong = HttpDateParser.parse("2012-04-17 16:09");
// conversion - long to string
Date dateObject = new Date(dateLong);
String dateStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(dateObject);

Resources