Convert normal string to datetime format - reactjs

Is there any way to convert normal string 5pm into 2022-04-20T17:00:00.000Z format?
I have got this from backend but Im using timepicker in antd. It only accepts 2022-04-20T17:00:00.000Z format and it is in string format in my DB.

This should consistently give you the current date with the time attached
function convertToISO(timeString) {
const [hour12, ampm] = timeString.split(/(?=[ap]m$)/i)
const hour = hour12 % 12 + (ampm.toLowerCase() === 'pm' ? 12 : 0)
const date = new Date()
// Set time, adjusted for time zone
date.setHours(hour, -date.getTimezoneOffset(), 0, 0)
return date.toISOString()
}
console.log(convertToISO('5pm'))

Related

How can I calculate the difference between two dates in Luxon?

I'm doing a project in React js and I need calculate the difference between dates.
The first one the user choose the startTime(state) and the endTime(state) and the program give the differecen in years, months and days ( this program is done and working).
Now I'm trying calculate the difference in days between the day 1-01-2022 and other date that the user choose.
I have tryed change the format but I receive errors.
//this is working
const date1 = DateTime.fromISO(endTime.toISOString());
const date2 = DateTime.fromISO(startTime.toISOString());
let durationDate = date1.diff(date2, ["years", "months", "days"]);
//--------------
//this is not working
const date3 = DateTime.fromISO(endTime.toISOString());
const dateNow = DateTime.now().startOf("year").toISO();
let dateTermination = endTime.diff(dateNow, ["years", "months", "days"]).toObject();
//----------------

How to create a datepicker in react js

I tried different method to develop date picker in reactJS. I done with "react-datepicker" package and it return "Wed May 15 2019 12:54:33 GMT+0100" as a result but I need 12/12/2000 format
try to format it as you want
const pickerDate = new Date('Wed May 15 2019 12:54:33 GMT+0100')
const day = pickerDate.getDay() < 10 ? '0' + pickerDate.getDay() : pickerDate.getDay()
const month = pickerDate.getMonth() + 1 < 10 ? '0' + (pickerDate.getMonth() + 1) : pickerDate.getMonth() + 1
const year = pickerDate.getFullYear()
const formatedDate = `${day}/${month}/${year}`
you can also use third part libraries like moment

Using Javascript slice inside angular expression

I receive a number representing a date in ddmmyy format eg.: 250615 i need transform into this format : 25/06/15.
I canot use the angular date filter because i receive a number.
I try to use slice in this way but only print the first number
{{ singularitem.Dia.slice(0,2)}} {{ singularitem.Dia.slice(2,2) }} {{ singularitem.Dia.slice(-2,2) }}
How can it be done ?
ThankĀ“s in advance
Since you can't use the date filter, just create your own filter, doing whatever you want it to do:
app.filter('transformDate', function() {
return function(date) {
// Split string at every second character and combine them again with a `/` in between
return date.match(/.{1,2}/g).join('/');
}
})
Usage:
{{date | transformDate}}
Plunker: http://plnkr.co/edit/uBeT0eHOoAlHDUWydOpM?p=preview
You can go for a vanilla javascript function. If your format is always dd/mm/yy check this function:
(function () {
var date = "250615";
date = date.slice(0, 2) + "/" + date.slice(2,4) + "/" + date.slice(4,6);
console.log(date);
})()
FIDDLE
Slice is a prototype function of Strings and Arrays, not numbers. You need to convert your number to string first.
If you want to convert that number to a date object, you'd need:
function parseDate(dateAsNumber) {
var dateAsString = "" + dateAsNumber;
var day = Number(dateAsString.substr(0, 2));
//months are index 0
var month = Number(dateAsString.substr(2,2)) - 1;
//assumming all dates are 2000+ since you only get 2 digits
var year = Number(dateAsString.substr(4,2)) + 2000;
var date = new Date(year, month, day);
return date;
}
Calling parseDate(250615) returns Thu Jun 25 2015 00:00:00 GMT-0700 (Pacific Daylight Time)

AngularJs filter sql timestamp "yyyy-MM-dd HH:mm:ss" to something else

I've tried the angular docs of {{someDate | date:'params'}}, however; This doesn't work if someDate is in this format yyyy-MM-dd HH:mm:ss.
How would I convert yyyy-MM-dd HH:mm:ss to say just yyyy using angularjs.
The date filter is designed to handle multiple input types including date objects and common date strings. If your date string is not recognized by the provided date filter, I would just write my own filter. It would look something like this...
myapp.filter('toYear', function () {
return function (dateString) {
var dateObject = new Date(dateString);
return dateObject.getFullYear();
};
});
You can use it like this...
{{someDate | toYear}}
You can define a custom format as follows
Date.prototype.customFormat = function(){
return this.getMonth() +
"/" + this.getDate() +
"/" + this.getFullYear();
}
//usage
> var d = new Date()
Fri Mar 14 2014 15:39:07 GMT+0000 (GMT Standard Time)
> d.customFormat()
'2/14/2014'
More details https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
I actually fixed the solution here:
app.filter('timestampToISO', function() {
return function(input) {
input = new Date(input).toISOString();
return input;
};
});
http://bit.ly/1iJAV8G
UPDATE:
To implement you add this to the view:
{{someSQLTIMESTAMP | timestampToISO | date}}
Angular js date object doesn't support to convert from MySql date format to timestamp directly. So need to convert it to acceptable format from YYYY-mm-dd H:i:s to YYYY/mm/dd H:i:s and then convert it to a timestamp.
You may try the below snippet to convert Mysql date format YYYY-mm-dd H:i:s to timestamp using AngularJs filter
app.filter("mysqlDateFormatToTimestamp", function(){
return function(date){
var date1 = date2 = date3 = timestamp = hours = minutes = seconds = '';
date1 = date.split(':');
date2 = date1[0].split(' ');
date3 = date2[0].split('-'); // Change it based on your format
if( date1.length == 1 && date2.length == 1 ){
hours = '00';
minutes = '00';
seconds = '00';
}else{
hours = parseInt(date2[1]);
minutes = parseInt(date1[1]);
seconds = parseInt(date1[2]);
}
timestamp = new Date(parseInt(date3[0]), parseInt(date3[1])-1, parseInt(date3[2]), hours, minutes, seconds);
return timestamp;
}
});
Now, You may use this filter wherever the date format you want, and you may change the params as you like.
{{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'LLLL dd, yyyy HH:mm:ss'}}
// results would comes like "July 07, 2016 05:27:30"
{{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'yyyy'}}
// results would comes like "2016"

How to set the Am and Pm value to the Time Picker In windows phone 7 C#

i am using time picker in windows phone to add the event
i am setting time ex 2.30 pm and i am stored the value in db
next in update page i am setting the value to timepicker the value getting form database
Code to set the value for time picker
`updateEventTimePicker.Value = new DateTime(Convert.ToInt32(eventdatee[2]), Convert.ToInt32(eventdatee[1]), Convert.ToInt32(eventdatee[0]), Convert.ToInt32(eventTimee[0]), Convert.ToInt32(eventTimee[1]),1);`
the problem is when i add in db the time was 2.30 pm but when i retrieve the value it sets 2.30 am how to set the AM and PM value
you cannot set the am or pm in the DateTime class. It is in a 24 hr format.
you can use this function i wrote to get your hour or minute in a 24 hr format.
public string [] get24hrFormat(string [] eventTime, string amOrpm)
{
amOrpm=amOrpm.ToLower();
string [] newTime= new string[2];
if(amOrpm=="am")
{
newTime[0]=eventTime[0];
newTime[1]=eventTime[1];
return newTime;
}
else
{
if (amOrpm == "pm")
{
if (eventTime[0] != "12")
{
newTime[0] = eventTime[0] + 12;
newTime[1] = eventTime[1];
}
else
{
newTime[0] = eventTime[0];
newTime[1] = eventTime[1];
}
}
return newTime;
}
}

Resources