Angular Date time conversion - html

I'm trying to convert the time and date from an html5 input to our countries format.
I'm trying to convert this: 2018-05-23 03:00
To this: 23-05-2018 03:00
I'm using angulars DatePipe like this:
var newDateBegin = this.dateFilter.transform(this.newDateBeginString.replace("T", " "), "dd-MM-yyyy HH:MM");
I have to replace the T in the string because for some reason the HTML5 input outputs it like this: 2018-05-23T03:00
But after converting it using: 2018-05-23 03:00, i get the following result:
2018-05-23 03:05.
No matter what time I put in there it always ends with xx:05. Am I missing something?
this is how i create the DatePipe:
private dateFilter: DatePipe = new DatePipe("nl-NL");

Isn't HH:MM the problem? M (uppercase) gets the month. Try to change it.
Month - MM - Numeric: 2 digits + zero padded
Use mm, as explained here
"dd-MM-yyyy HH:mm"

Related

Angular - using a date pipe with ngModel

I am getting the following error in my console - "Error: InvalidPipeArgument: 'Unable to convert "[object Object]" into a date' for pipe 'DatePipe'".
I have a calendar input, which should allow the user to select a date and then pass on that date in a certain format, 'dd/MM/yyyy'. I want the date selected to show in the calendar input once they have selected a date.
I realise I cannot have two way binding on the [ngModel] if I have a pipe there so I'm using
(ngModelChange). If I remove the #createdByCutOffDate="ngModel" then the error is removed but I cannot see the selected date show in the calendar input.
I also tried the updateCreatedByCutOffDate() method taking a date type or string.
this.createdByCutOffDate is in the following format - 'Thu Feb 17 2022 00:00:00 GMT+0000 (Greenwich Mean Time)'
component.html
<input type="date"
id="createdByCutOffDate"
[ngModel]="createdByCutOffDate | date:'dd/MM/yyyy'"
#createdByCutOffDate="ngModel"
(ngModelChange)="updateCreatedByCutOffDate($event)" />
component.ts
createdByCutOffDate: Date;
updateCreatedByCutOffDate(date: string) {
this.createdByCutOffDate = new Date(date);
}
createdByCutOffDate is a Date object that has its methods and properties.
So to solve your problem, use "createdByCutOffDate.date | date:'dd/MM/yyyy'" instead of "createdByCutOffDate | date:'dd/MM/yyyy'"

How can I convert a hour range to a total number of hours using SQL?

I'm currently working with Yelp's dataset and my objective is to find the total hours a business is open a week. From the data, I am able to extract a time range for each day that looks like "9:0-0:0" or "17:30-21:30" for a certain day. How can I use SQL to get a single value for these hourly ranges?
This is some code I used to simply show the hours for a business in the dataset.
df_busSelected = df_business.select("business_id", "name"
, "hours.Sunday", "hours.Monday", "hours.Tuesday"
,"hours.Wednesday", "hours.Thursday", "hours.Friday"
, "hours.Saturday")
df_busSelected.show()
I hope to make a new column with the total hours a business is open a week with the given ranges.
Using pyspark, you can do something like the following:
from pyspark.sql.functions import expr
df.withColumn('h', expr('split(hours, "[-:]")').astype('array<int>')) \
.withColumn('h1', expr('''
h[2] - h[0] + IF(h[2]<h[0],24,0) + round((h[3]-h[1])/60,0)
''').astype('int')) \
.show()
+----------+-----------+----------------+---+
| bdate| hours| h| h1|
+----------+-----------+----------------+---+
|2019-10-17| 9:0-0:0| [9, 0, 0, 0]| 15|
|2019-10-18|17:30-21:30|[17, 30, 21, 30]| 4|
+----------+-----------+----------------+---+
First use the charset [-:] to split the column hours into an array of 4 items (named h) and cast it into array of int (originally array of StringType)
Then do the simple math and at the end cast the result type to int again.
Next, use date_format('bdate', 'EEEE') to get week name and then use groupby and pivot and probably agg + sum functions to get the final result

how to change date format in mongodb

I have created mongodb collection using nodejs as below:
var mongoose = require('mongoose');
var TTTUsersSchema = new mongoose.Schema({
username: String,
password:String,
active: Boolean,
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
});
module.exports = mongoose.model('Users', TTTUsersSchema);
Since creating record the default date is stored as Tue Nov 13 2018
20:53:47 GMT+0000 (GMT Standard Time) so when I fetch this field then it
is displayed in HTML as above. I want to display it in DD/MM/YY HH:MM
format.
Where I need to change? In HTML UI level? or at Mondgodb collection level.
Please help.
Time stamps should always be stored in a consistent way, e.g. like ISODate() in MongoDB, and always be handled in the code in a consistent way, e.g. like Date object in Javascript.
Only when you
present a time stamp to a user as string, or
parse a time string from the user
then you do conversions.
Unfortunately JavaScript Date object is severely limited, so I would suggest to use a package like Moment.js which offers lots of formatting capabilities:
const moment = require('moment');
// date is a Date object you got, e.g. from MongoDB
const time = moment(date);
console.log(time.format("DD/MM/YY HH:mm"));
You need to format your date time at view layer.
one option is to format at your controller if you are using MVC or at the layer that produce view model.
the another option is to format at html using javascript. You may need to use lib like moment.js
Assume current day is Tue Nov 13 2018 20:53:47 GMT+0000
Use .toDateString()
var date = new Date();
console.log(date.toDateString());
It will display:
Tue Nov 13 2018

convert Date Format yyyy-mm-dd to dd-mm-yyyy in flex

I have a requirement. I have a date format as yyyy/mm/dd and I want to convert this format to mm/dd/yyyy in action script.
I had tried to parse to this format, it is not working. Can any one of you please help me?
var s:String = "2012/03/05";
var dateParts:Array = s.split('/'); //['2012', '03', '05']
//take off the first element and add it back at the end
dateParts.push(dateParts.shift()); //['03', '05', '2012']
s = dateParts.join('/'); //'03/05/2012'
trace(s);
If we're opting for string manipulation, why not this:
'2010/05/14'.replace(/(\d{4})\/(\d{2})\/(\d{2})/, '$2/$3/$1')

jqGrid Doesn't Sort When Showing Epoch Time (since as milliseconds) as Date

I use jqGrid and my grid definition is like that:
...
colNames:['Type','Date','Message','User Name','Host'],
colModel:[{name:'type',index:'type', width:100},
{name:'date',index:'date', sorttype:'date', formatter:'date',
formatoptions: {newformat:'d-M-Y'}, width:100},
{name:'log',index:'log', width:200},
{name:'username',index:'username', width:50},
{name:'host',index:'host', width:50}],
...
When I debug my coming data one of the date value (it is Number) is as follows:
1322550786997
Grid shows it like that:
29-Nov-2011
Everything is OK till this point. However when I want to sort my date column it doesn't change anything.
Any ideas?
The problem is that decoding of the Unix (formatoptions: {srcformat: 'U', newformat: 'd-M-Y'}) date 1322550786997 get us 19-Dec-43879 and not 29-Nov-2011. You correct representation of the date will be the string "\/Date(1322550786997)\/" instead of the number 1322550786997.
See the demo:
UPDATED: You can also use the following custom formatter as a workaround
formatter: function (cellval, opts) {
var date = new Date(cellval);
opts = $.extend({}, $.jgrid.formatter.date, opts);
return $.fmatter.util.DateFormat("", date, 'd-M-Y', opts);
}
It creates Date and then use original date formatter to convert it to the format 'd-M-Y'. See here the demo.