How to combine date and time into a single datetime object? - javascript

In my react native app i need to combine my date and time in to single datetime object. I used react native modal date time picker npm package for get date and time.
I have a datepicker returning a date string, and a timepicker returning a time string. When i try to combine it will give me a output as Invalid date.
concatDateTime = () => {
var date = this.state.date;
var time = this.state.currentTime;
var dateTime = Moment(date + ' ' + time, 'DD/MM/YYYY HH:mm');
console.log(dateTime.format('YYYY-MM-DD HH:mm'));
}
I need dateobject in ('YYYY-MM-DDTHH:mm:s') format.

You can specify the format of your input string to let moment know how to parse it.
var date = '2019-02-16';
var time = '8:24 PM';
// tell moment how to parse the input string
var momentObj = moment(date + time, 'YYYY-MM-DDLT');
// conversion
var dateTime = momentObj.format('YYYY-MM-DDTHH:mm:s');
console.log(dateTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Just click on below link,
https://stackblitz.com/edit/typescript-bmgx2p?file=index.ts
I hope it will solve your problem.

Another alternative:
let mDate = moment(data.StartDateLocal).tz("Australia/Melbourne");
let mTime = moment(data.StartTimeLocal).tz("Australia/Melbourne");
let x1 = {
'hour': mTime.get('hour'),
'minute': mTime.get('minute'),
'second': mTime.get('second')
}
mDate.set(x1);
this._json.header.transactionStartDateTime = mDate.format("YYYY-MM-DDTHH:mm:ss");

Related

From date to date validation using javascript

I want to validate from and to date. my date format is d/m/Y H:i
Here is my code:
var startDate = new Date($('#fromdate').val());
var endDate = new Date($('#todate').val());
if (endDate.getTime() <= startDate.getTime()) {
return [false,"To Date cannot be less than From Date"];
}else{
return [true,""];
}
result showing 'Invalid Date'.
Here the date format is different. How to change the date format before passing to Date function?.
You can parse the date string on your own or you can use an external library, like dayjs or momentjs
A simple parsing function could be something like this (assuming the format is the one you mentioned in your question):
function getDateFromCustomFormat(dateString) {
const dateFormatPattern = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2})$/
const parts = dateString.match(dateFormatPattern)
console.log(parts)
const validFormatString = `${parts[3]}-${parts[2]}-${parts[1]} ${parts[4]}:${parts[5]}`
new Date(validFormatString)
}

moment timezone returns bad value for unit test

So my project is using moment timezone. And it works perfectly everywhere its used except in a unit test. And I can't figure out why.
I'm calling
moment(new Date(date)).tz(timezone).format(mask);
where the date, mask, and timezone are as follows:
date = "2016-11-11T19:34:56.601Z"
mask = "mm-dd-yyyy"
timezone = "America/New_York"
The result I get is: 34-Fr-yyyy
In another call, the following occurs:
date = "2016-12-13T21:57:53.336Z"
mask = "mm-dd-yyyy"
timezone = "America/New_York"
and the result is 57-Tu-yyyy
You use the wrong date format, the correct is MM-DD-YYYY.
var date = "2016-12-13T21:57:53.336Z";
var mask = "mm-dd-yyyy";
var timezone = "America/New_York";
var dateStr = moment(date).tz(timezone).format('MM-DD-YYYY');
console.log(dateStr);
<script src="http://momentjs.com/downloads/moment.js"></script>
<script src="http://momentjs.com/downloads/moment-timezone-with-data.js"></script>
http://momentjs.com/docs/#/parsing/string-format

Get custom date time format in moment.js

I have time saved in database like this
2016-01-30 14:36:00
But I am using moment.js and I want to convert the time as in this format
2016-01-30T14:36:00
So to get that format I tried like this
var date = new Date();
console.log(moment(date).parseZone().format());
but its showing date like this
2015-12-21T15:20:04+05:30
So can someone tell me how to get the time in that format in moment.js
try this
var dbDate = '2016-01-30 14:36:00';
var format = 'YYYY-MM-DDTHH:mm:ss';
var date = moment(dbDate, format);
var isValid = date.isValid() // true
console.log(isValid, date.year(), date.month(), date.date())
Try using the below code -
console.log(moment(new Date()).format('YYYY-MM-DD[T]HH:mm:ss'));
Use a custom format:
var date = moment();
var out = date.format('YYYY-MM-DDTHH:mm:ss');
console.log(out);
This will help you
var currentDate = moment(new Date()).format('YYYY-MM-DDTHH:mm:ss');
//2015-12-21T15:50:28

Convert date format 27.05.2015 01:46:32.UTC to Locale time

Can anyone help me to convert date format 27.05.2015 01:46:32.UTC to Locale time.
I tried the following code
var date = new Date('27.05.2015 01:46:32.UTC ');
date.toString();
This is also not working.
Even momentjs is also gives error to convert this format of date.
As per statement even momentjs is also gives error to convert this format of date, I have taken liberty and used momentjs here.
You need to use string-format moment constructor to pass string and format in which input string is.
Use
var t = "27.05.2015 01:46:32.UTC";
//pass dateTime string and its format, it will return
//moment object
var cdt = moment.utc(t, 'DD.MM.YYYY HH:mm:ss');
//Get date object
var date = cdt.toDate();
//Use date variable as per requiremnt
alert(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
The format you are using for Date is DD.MM.YYYY. So you have to change this to MM.DD.YYYY for a valid java script object.
var date = '27.05.2015 01:46:32.UTC';
var date1 = date.split(' ');
console.log(date1)
var date2 = date1[0].split('.');
console.log(date2)
var date3 = date2[1]+ '.' +date2[0] +'.'+ date2[2];
console.log(date3)
var final_date = date3 + ' ' + date1[1];
console.log(final_date);
final_date.toString();

Convert datetime object into specific string Javascript

I have a date in the format dd.MM.yyyy HH:mm, for example 31.01.2014 11:24. How can I obtain, in javascript, the string "/Date(1391160281569)/"?
Here is an approach
var date = "31.01.2014 11:24";
var sp1 = date.split(/[:. ]/);
var newDate = new Date(sp1[2], (+sp1[1] - 1), sp1[0], sp1[3], sp1[4]);
var milliSeconds = newDate.getTime();
var urFormat = "/Date(" + milliSeconds + ")/";
alert(urFormat)
JSFiddle
I took me a while but i got this:
var theStringVersion = "/Date("+$.now()+")/";
Of course, for a real date, i would have to get the timestamp for it.

Categories

Resources