convert string to Default date Format - javascript

Hi I have use the string object for represent the date in the following format
var date="18/01/2011";
var dateFormat="dd/MM/yyyy";
Note:
In my scenorio i have use the dd/MM/yyyy format;
dateFormat will be different in my client side.
how to convert these date default JavaScript dateFormat as MM/dd/yyyy in Generic way.
I have tried in by split date by and swap the month ,date to achieve this requirement. But in my client side i dont know about the format of the date how to convert any other format to default Javascript format

Hope this helps:
var date="18/01/2011";
var parts = date.split('/');
var result = new Date(parts[2], parts[1], parts[0]);

var datestring = "2014-02-27:04:05";var d = Date(datestring);

Related

How to convert a date string from 'dd//mm/yy' to 'mm/dd/yy' in javascript?

I am using a library to calculate the age from date of birth. I am taking date of birth as an input which is in the format of dd/mm/yy but the library that calculated the age accepts it in the format of mm/dd/yy. One solution to this is to change the date selector format in the application but I dont want to do that since it gets confusing.
I searched the solution on stackoverflow but couldnt find the solution here- How to format a JavaScript date
How about a simple split and join:
var yourDate = "10/12/2021";
var arrayOfDate = yourDate.split("/");
console.log([arrayOfDate[1], arrayOfDate[0], arrayOfDate[2]].join('/'));
Just split it with / and then use destructure it and get the desired result as:
const result = `${mm}/${dd}/${yy}`;
var oldDate = "10/12/21";
var [dd, mm, yy] = oldDate.split("/");
const newDate = `${mm}/${dd}/${yy}`;
console.log(newDate);

How do you convert a timestamp format within Javascript?

how do I convert something like
2020-12-01T15%3A48%3A39.862Z
to a timestamp format of
2018-09-15T15%3A53%3A00-07%3A00?
I am not very familiar with timestamp formats and manipulation of datetime in Javascript...
You can turn it into a DateTime directly, you just have to decode all that %3A stuff first:
var timeString = '2020-12-01T15%3A48%3A39.862Z';
var decodedTimestring = decodeURIComponent(timeString);
var date = new Date(decodedTimestring);
console.log(date.toLocaleString());

How to get date from js in correct format? Month and days get reversed

I am calling an ajax for getting some values for editing data.
As a part of my object, I am sending the date field.
My problem is that when I receive the date value in the controller, date format is wrong - my dates and months are reversed. And because of that I can't compare them where I need to.
But my months and days are reversed. For an example , instead of 3rd October, it returnes 10th of March.
How to fix this?
I am sending the date field from js in a object like this:
ExamsDataU = {
classId: classIdValue,
date: dateValue
};
And in my controller I tried:
DateTime dateToCheck = Convert.ToDateTime(dto.Date);
The first thing you should know is date parsing with Convert.ToDateTime() depends to the current culture used in server (you may check it using CultureInfo.CurrentCulture property). You can try one of these methods to parse JS date format properly inside controller action method:
1) Using DateTime.ParseExact()/DateTime.TryParseExact() with custom format
On this way it is necessary to specify date format before parsing date:
// specify custom format
string dateFormat = "dd-MM-yyyy";
DateTime dateToCheck = DateTime.ParseExact(dto.Date, dateFormat, CultureInfo.InvariantCulture);
2) Using DateTime.ParseExact()/DateTime.TryParseExact() with ISO 8601 format
Use date: dateValue.toISOString(); to convert JS date into ISO 8601 format and then convert it:
// specify ISO format
string dateFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";
DateTime dateToCheck = DateTime.ParseExact(dto.Date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
This way is better than the former because no need to write additional date representation code in client-side, also you can adjust date representation to local time if necessary.
Notes:
a) For specified culture, you can try CultureInfo.GetCultureInfo():
var culture = CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name);
DateTime dateToCheck = DateTime.ParseExact(dto.Date, dateFormat, culture);
b) You can use if condition to check if the date string is valid when using DateTime.TryParseExact():
DateTime dateToCheck;
if (DateTime.TryParseExact(dto.Date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateToCheck))
{
// do something
}
Try this
var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yy');
or
var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[1] + '-' + dateAr[2] + '-' + dateAr[0].slice(-2);
console.log(newDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Convert systematic date to formatted date

I want to convert systematic date into readable date format. However when I pass systematic date as argument to date constructor I get Invalid date response. How to do this properly in order to display formatted date such as dd-mm-yyyy for GMT+2 ?
var date = message.date; // => 1466663308000
var dateObject = new Date(date);
console.log(dateObject);
Console output:
Invalid Date
You have to make sure the timestamp value is a number and not a string:
var date = message.date;
var dateObject = new Date(+date); // note the +
console.log(dateObject);
Once you've got a valid date, there are many other questions here about formatting dates.
I tried the code, it is absolutely correct.
I can get the correct date
var d=new Date(1466663308000);
document.write(d);
But I tried another way:
var x = "1466663308000";
var d=new Date(x);
document.write(d);
I got the "Invalid date", so I guess, message.date should be a string, please try to long(message.date).

How to get date format in Angular-JS

My requirement is something different. I want to get the date format, not to format the date. Means I have a date string and now I want to get the date format of that date and apply it to the another date as a format.
Let me explain in brief with example:
var dateStr = "2015-06-06T12:00:00Z";
var d = new Date(dateStr);
here my date format is yyyy-MM-ddTHH:mm:ssZ you can see in dateStr object.
Now i will create another date and want to apply the same date-format to this new date.
var formatStr = "yyyy-MM-dd'T'HH:mm:ss'Z'"; // want to get this from above date, not hard coded like this.
var newDate = $filter('date')(d, formatStr);
here you can see that i have hard coded the format string, which i don't want to do. Here this string should be come from the above d date/or dateStr String.
You can do it by using momment.js
http://momentjs.com/downloads/moment.js
van date=new Date(date);
var dateInFormate=moment(date);
var date=dateInFormate.format('yyyy-MM-ddTHH:mm:ssZ');
As #Rob said, there is doubt on the reliably for all formats. What you need is pre defined map with key being the format and value being its corresponding regular expression.
Now, create a function with input as dateStr and will return the format. Like
function getDateFormat(dateStr) {
var format = default_format;
// Check in map for format
// If you get the format in map, return that else return a default format.
return format;
}

Categories

Resources