How to format Date Time in JavaScript? [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I have a date timestamp coming in this format 2021-02-08T19:36:47.000+0000. How can I format this in JavaScript in DD-MM-YYYY Hh:mm format.

Download moment.js and include to your file
then you can use like this
moment("2021-02-08T19:36:47.000+0000).format("DD-MM-YYYY Hh:mm")
I usually use this in my every project
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js

This is plain Javascript
// 2021-02-08T19:36:47.000+0000
function formatDate(date) {
var d = new Date(date),
minutes = ':' + d.getMinutes(),
hours = ' ' + d.getHours(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = '-' + d.getFullYear();
if (month.length < 2) {
month = '-0' + month;
} else if (month.length < 3) {
month = '-' + month;
}
if (day.length < 2)
day = '0' + day;
// DD-MM-YYYY HH:mm
return [day, month, year, hours, minutes].join('');
}
console.log(formatDate("2021-02-08T19:36:47.000+0000"))

Related

Convert timestamp to yyyy-mm-dd hh:mm:ss format using javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How to format a Date in MM/dd/yyyy HH:mm:ss format in JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I need to convert the current timestamp (Eg: 1578293326452) to yyyy-mm-dd hh:mm:ss format
using javascript.
I obtained the current timestamp as follows:
var date = new Date();
var timestamp = date.getTime();
How can I change the format?
function getTime(){
var date = new Date();
var year = date.getFullYear();
var month = (date.getMonth() +1);
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return formateTime(year, month, day, hour, minute, second);
}
function formateTime(year, month, day, hour, minute, second){
return makeDoubleDigit(year) + "-" +
makeDoubleDigit(month) + "-" +
makeDoubleDigit(day) + " " +
makeDoubleDigit(hour) + ":" +
makeDoubleDigit(minute) + ":" +
makeDoubleDigit(second);
}
function makeDoubleDigit(x){
return (x < 10) ? "0" + x : x;
}
console.log(getTime())
Maybe this is what you need
d = Date.now();
d = new Date(d);
d = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+' '+(d.getHours() > 12 ? d.getHours() - 12 : d.getHours())+':'+d.getMinutes()+' '+(d.getHours() >= 12 ? "PM" : "AM");
console.log(d);

Converting to appropriate date format from a given long date

I have a date in format like that -
date= "2 march 2018"
I want to convert it to the format "20180302". i am using following method for that :
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
return [year, month, day].join('');
Everything works fine except that if my date has no date or month, it should return empty value in place of date and month. For example: If my date input is
date= "march 2018"
than, day = '' + d.getDate() should return empty. if date is "2018" than month = '' + (d.getMonth() + 1) and day = '' + d.getDate() should return null. I know it is a bad practise,but in my application I need to check if date and month is present of not..
var date= "2 march 2018";
var sp_date=date.split(" ");
var d = new Date(date);
if(sp_date.length==3){
month = '' + (d.getMonth() + 1);
day = '' + d.getDate();
year = d.getFullYear();
}else if(sp_date.length==2){
if(isNaN(Number(sp_date[0]))===false){
day = sp_date[0];
month='';
year=sp_date[1];
}else{
day ='';
month = '' + (d.getMonth() + 1);
year = d.getFullYear();
}
}else if(sp_date.length==1){
day ='';
month = '';
year = d.getFullYear();
}
return [year, month, day].join('');
You can do it with a single line using moment.js, Check the jsfiddle link below
http://jsfiddle.net/rLjQx/46813/
var date = moment("2 march 2018").format('YYYYMMDD');

web api not receiving date from angularjs

Hi I am developing web application in angularjs. I have one date in angularjs for example 13-10-2017. In c# i have below field
public DateTime LicenseExpiryDate { get; set; }
When i send 13-10-2017 in ajax request,LicenseExpiryDate accepts as 0001-01-01. May i know how to fix this? I can see my angular date is in dd-mm-yy format and c# date default format is yyyy-mm-dd.
i tried to convert it to yyyy-mm-dd as below
function formatDateDOsetyymmdd(date) {
debugger;
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year,month, day].join('-');
}
and this returns NaN-NaN-NaN
Any help would be appreciated. Thank you.
You are getting a string in form dd-mm-yyyy on input, what you want to do is turn that string into three numbers, and use those in the new Date call
function formatDateDOsetyymmdd(date) {
date = date.split('-').map(Number);
var d = new Date(date[2], date[1] - 1, date[0]),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year,month, day].join('-');
}
console.log(formatDateDOsetyymmdd('13-10-2017'));
you can use moment.js , if you can't do this in JavaScript means you can use it.
https://momentjs.com/

javascript date format conversion [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I am using pikaday's datepicker. It gives me output "Fri Sep 20 2013". How can I convert this date into yyyy-mm-dd format and I also would want following date of this selected date and set that one to another element.
I tried this code
function formattedDate() {
var fromdate = new Date(document.getElementById('datepicker').value);
var dd = fromdate.getDate();
var mm = fromdate.getMonth()+1; //January is 0!
var yyyy = fromdate.getFullYear();
if(dd < 10)
{
dd = '0'+ dd;
}
if(mm < 10)
{
mm = '0' + mm;
}
var fromdate1 = dd+'/'+mm+'/'+yyyy;
fromdate.setDate(fromdate.getDate() + 2);
document.getElementById('datepicker').value = fromdate1;
var newdate = fromdate;
document.getElementById('datepicker1').value = newdate;
//alert(newdate1);
}
But it doesn't work.
var date = new Date(dateString);
var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var properlyFormatted = "" + year + month + day;
Or
var date = new Date(dateString);
var properlyFormatted = date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2);
Use momentjs — it's a library available for using in browser and node projects
In your case you should use this pattern:
moment().format("YYYY-mm-D");
And you can try it in console on momentjs's site:

Javascript change date format from YYYY-MM-DD HH:MM:SS to MM-DD-YYYY [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I want to change YYYY-MM-DD HH:MM:SS to MM-DD-YYYY
Example:
I have a string which represents a date in this format: 2013-06-15 03:00:00
I want to change this string into 06-15-2013 using JavaScript.
Is there any library to do this? or should i just use JavaScript?
function change(time) {
var r = time.match(/^\s*([0-9]+)\s*-\s*([0-9]+)\s*-\s*([0-9]+)(.*)$/);
return r[2]+"-"+r[3]+"-"+r[1]+r[4];
}
change("2013-06-15 03:00:00");
see moment.js.. i found it very useful
http://momentjs.com/
DEMO: http://jsfiddle.net/abc123/f6k3H/1/
Javascript:
var d = new Date();
var c = new Date('2013-06-15 03:00:00');
alert(formatDate(c));
alert(formatDate(d));
function formatDate(d)
{
var month = d.getMonth();
var day = d.getDate();
month = month + 1;
month = month + "";
if (month.length == 1)
{
month = "0" + month;
}
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
return month + '-' + day + '-' + d.getFullYear();
}
Since this doesn't use RegEx you'll notice some weirdness....Examples:
d.getMonth() + 1
this is because getMonth is 0 indexed....
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
this is because hours, seconds, minutes all return 1 digit when they are 1 digit...this makes them return a leading 0. The same can be done to Month and Day if desired.

Categories

Resources