Converting Date field and Character Time field to DateTime - visual-foxpro

I have two fields that I am trying to combine into a DateTime field. The first field is a DATE field and the second field is a character field that has the time in 24hour format.
For example:
field1: 10/02/13
field2: 19:58:18
How can I get this into a DATETIME field?

The DATETIME() function will do it for you:
mdate = DATE()
mtime = TIME()
? DATETIME(YEAR(mdate), MONTH(mdate), DAY(mdate), VAL(LEFT(mtime, 2)), VAL(SUBS(mtime, 3, 2)), VAL(RIGHT(mtime, 2)))

Related

how to extract the today's date out of the date column using SQL query?

I assume I would need to change query in order to sort the data with today's date.
Please tell me how to change it though...
SQL QUERY in ToDoDao
#Query("SELECT * FROM todo_table WHERE date(date) = date('now')")
fun getTodayList(): Flow<List<ToDoTask>>
DATABASE
#Entity(tableName = DATABASE_TABLE)
data class ToDoTask(
#PrimaryKey(autoGenerate = true) val id: Int = 0,
#ColumnInfo(name = "title") val title: String,
#ColumnInfo(name = "description") val description: String,
#ColumnInfo(name = "priority") val priority: Priority,
#ColumnInfo(name = "date") val date: String,
#ColumnInfo(name = "favorite") var favorite: Boolean)
date val in ViewModel class
val date : MutableState<String> = mutableStateOf("")
datas inserted
enter image description here
I have tried the code below and I was able to activate the function as the query as I intented, so I think the query is the issue here.
#Query("SELECT * FROM todo_table WHERE date = '2023-2-14'")
fun getTodayList(): Flow<List<ToDoTask>>
The Issue
The issue is that the SQLite date function expects the date to be in an accepted format.
YYYY-M-DD is not such a format and will result in null rather than a date. YYYY-MM-DD is an accepted format (see https://www.sqlite.org/lang_datefunc.html#time_values). That is leading zeros are used to expand single digit numbers to 2 digit numbers for the month and day of month values.
The Fix (not recommended)
To fix the issue you have shown, you could use (see the However below):-
#Query("SELECT * FROM todo_table WHERE date(substr(date,1,5)||CASE WHEN substr(date,7,1) = '-' THEN '0' ELSE '' END ||substr(date,6)) = date('now');")
If the month was 2 numerics i.e. MM (e.g. 02 for February) then the above would not be necessary.
The CASE WHEN THEN ELSE END construct is similar to IF THEN ELSE END. see https://www.sqlite.org/lang_expr.html#the_case_expression. This is used to add the additional leading 0, when omitted, to the string used by the date function.
However, the above would not cater for days that have the leading 0 omitted for the first 9 days of the month. This due to the 4 permutations of the format (YYYY-MM-DD, YYYY-MM-D, YYYY-M-D and YYYY-M-DD) would be more complex e.g.
#Query("SELECT * FROM todo_table WHERE date(CASE WHEN length(date) = 8 THEN substr(date,1,5)||'0'||substr(date,6,1)||'-0'||substr(date,8) WHEN length(date) = 9 AND substr(date,7,1) = '-' THEN substr(date,1,5)||'0'||substr(date,6) WHEN length(date) = 9 AND substr(date,8,1) = '-' THEN substr(date,1,8)||'0'||substr(date,9) ELSE date END) = date('now');")
Recommended Fix
The recommended fix is to store values using one of the accepted formats rather than try to manipulate values to be an accepted date to then be worked upon using the date and time functions.

How to convert date time to UNIX in PowerBI for Desktop

I am new to powerBI I want to convert DateTime.LocalNow() to UNIX Timestamp
Use Duration.TotalSeconds
Duration.TotalSeconds(DateTime.LocalNow() - DateTime.FromText("1/1/1970"))
You can use this function:
let
fun_DateTimeToUnix = (optional DateTime as nullable datetime) as number =>
let
default_DateTime = if DateTime is null then DateTime.From(DateTimeZone.UtcNow()) else DateTime,
UnixDateTime = Duration.TotalSeconds(default_DateTime - DateTime.FromText("1/1/1970"))
in
UnixDateTime
in
fun_DateTimeToUnix
As shown:

Print ZonedDateTime exactly as it is parsed

I have a date string in a format:
String date = 2014-05-05T05:05:00.000
ZonedDateTime zoneDateTime = LocalDateTime.parse(date).atZone(ZoneId.of("UTC");
the above prints:
2014-05-05T05:05Z[UTC]
Is there any way we can print it in the following format ?
2014-05-05T05:05:00.000Z
In joda time, i can easily do this:
DateTime datetime= org.joda.time.LocalDateTime.parse(date).toDateTime(UTC)
Is there any way we can print it in the following format ?
String date = 2014-05-05T05:05:00.000
You can use java.time.format.DateTimeFormatter.ofPattern().
String date = "2014-05-05T05:05:00.000";
ZonedDateTime zoneDateTime = LocalDateTime.parse(date).atZone(ZoneId.of("UTC"));
System.out.println(zoneDateTime);
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX").format(zoneDateTime));
Refrence: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

combine Date and Time into a DateTime from api response

What is the efficient way to combine Date and Time (strings) into a single DateTime? i am using football-api in response i am getting time attribute in "08:50" these format and date attribute "01.01.2018" these format. I want to save in database 2018-01-01 08:50:00 format in date field.
a = match["formatted_date"].to_date.strftime("%Y, %m, %d")
y = a[0..3].to_i
m = a[6..7].to_i
d = a[10..11].to_i
date = Date.new(y, m, d).to_datetime + Time.parse(match["time"]).seconds_since_midnight.seconds

Python Datetime string parsing: How to know what has been parsed?

I want to use Python to parse a user input string and need to know which portion of date has been specified, e.g.,
"Jan. 2017" => Month = 1, Year = 2017
The result should tell me only month and year are specified in the string, and return those values, while:
"2003-05-01"
specifies day, month and year.
I tried to use dateutil.parser.parse, and gave it a rare default date value. e.g., 1900/01/01, and then compare the parsed result with the default date and see the difference. But if the month or day are both 1 in the parsed result,
it needs one more round of parsing with a different default value, in order to rule out the possibility of it being the default value, or from the user input.
The above way seems quirky. Is there a library for me to parse commonly used date string format and knowing what has been parsed?
I ended up doing the quirky way:
from dateutil.parser import parse
# parse the date str, and return day/month/year specified in the string.
# the value is None if the string does not have information
def parse_date(date_str):
# choose two different dates and see if two parsed results
default_date1 = datetime.datetime(1900, 1, 1, 0, 0)
default_date2 = datetime.datetime(1901, 12, 12, 0, 0)
year = None
month = None
day = None
try:
parsed_result1 = parse(date_str, default=default_date1)
parsed_result2 = parse(date_str, default=default_date2)
if parsed_result1.year == parsed_result2.year: year = parsed_result2.year
if parsed_result1.month == parsed_result2.month: month = parsed_result2.month
if parsed_result1.day == parsed_result2.day: day = parsed_result2.day
return year, month, day
except ValueError:
return None, None, None

Resources