Get custom date time format in moment.js - javascript

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

Related

How do I convert date mm/dd/yyyy to yyyy-mm-ddT00:00:00.000Z format in javascript

var dateType = $filter('date')($scope.datalist[0].OVERRIDE_DATE, "MM/dd/yyyy");
I have the output : 05/07/1992
I want to change above date to yyyy-mm-ddT00:00:00.000Z format ?
toISOString() should do what you want.
e.g.
new Date('05/07/1992').toISOString();
will give you
"1992-05-07T07:00:00.000Z"
The Date.prototype.toISOString() method may be what you want:
const rawDate = $filter('date')($scope.datalist[0].OVERRIDE_DATE, "MM/dd/yyyy");
const formattedDate = new Date(rawDate).toISOString();
or, if $scope.datalist[0].OVERRIDE_DATE is already a date object, you could just do:
const formattedDate = $scope.datalist[0].OVERRIDE_DATE.toISOString();
Link to Date.prototype.toISOString docs

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

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");

Convert DD-MM-YYYY format to MM-DD-YYYY in moment.js

I am trying to convert a date from DD-MM-YYYY to MM-DD-YYYY. But it doesn't work for me. I tried this
var test = Date.parse('15-06-2010');
var SearchDate = moment('15-06-2010').format("MM-DD-YYYY");
var day = moment(new Date("15-06-2010"), "MM-DD-YYYY");
console.log(test);
console.log(day);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
Help me.
Try this:
var date = moment('15-06-2010', 'DD-MM-YYYY')
console.log(date.format('MM-DD-YYYY'))
You have to specify the input format for the date when parsing a date with momentjs.
Here 26-11-2019 will be converted to 11-26-2019
const date = moment('26-11-2019','DD-MM-YYYY').format('MM-DD-YYYY');
To the format method we can give any format, how you want the output.
Try this:
moment(moment('15-06-2010', 'DD-MM-YYYY')).format('MM-DD-YYYY')
I have to validate the date using momentjs. So following works for me :).
var dateValidate = moment(startDate).isValid() || moment(startDate, "DD/MM/YYYY").isValid() ? true : false;
Try something like this,
var formatDate= 1399919400000;
var responseDate = moment(formatDate).format('MM-DD-YYYY');

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